Core starter providing fundamental building blocks for Spring Boot applications with auto-configuration support, logging, and YAML parsing
—
Database connectivity, transaction management, and ORM integration with support for JDBC, JPA, R2DBC, and database migration tools.
Core JDBC utilities and data source management.
/**
* Convenience class for building a DataSource
*/
public final class DataSourceBuilder {
/**
* Get a DataSourceBuilder
* @return a new data source builder instance
*/
public static DataSourceBuilder create();
/**
* Get a DataSourceBuilder based on the specified class loader
* @param classLoader the class loader used to create the builder
* @return a new data source builder instance
*/
public static DataSourceBuilder create(ClassLoader classLoader);
/**
* Return a new builder based on the state of this builder but with the specified type used for the DataSource
* @param type the DataSource type
* @return a new builder
*/
public DataSourceBuilder type(Class<? extends DataSource> type);
/**
* Return a new builder based on the state of this builder but with the specified URL
* @param url the JDBC URL
* @return a new builder
*/
public DataSourceBuilder url(String url);
/**
* Return a new builder based on the state of this builder but with the specified username
* @param username the username
* @return a new builder
*/
public DataSourceBuilder username(String username);
/**
* Return a new builder based on the state of this builder but with the specified password
* @param password the password
* @return a new builder
*/
public DataSourceBuilder password(String password);
/**
* Return a new builder based on the state of this builder but with the specified driver class name
* @param driverClassName the driver class name
* @return a new builder
*/
public DataSourceBuilder driverClassName(String driverClassName);
/**
* Build a DataSource with the current state of this builder
* @return a DataSource
*/
public <T extends DataSource> T build();
}
/**
* Common database drivers used by Spring Boot
*/
public enum DatabaseDriver {
/**
* Apache Derby database driver
*/
DERBY("org.apache.derby.jdbc.EmbeddedDriver", "org.apache.derby.jdbc.EmbeddedXADataSource"),
/**
* H2 database driver
*/
H2("org.h2.Driver", "org.h2.jdbcx.JdbcDataSource"),
/**
* HyperSQL database driver
*/
HSQLDB("org.hsqldb.jdbc.JDBCDriver", "org.hsqldb.jdbc.pool.JDBCXADataSource"),
/**
* SQLite database driver
*/
SQLITE("org.sqlite.JDBC"),
/**
* MySQL database driver
*/
MYSQL("com.mysql.cj.jdbc.Driver", "com.mysql.cj.jdbc.MysqlXADataSource", "jdbc:mysql:"),
/**
* MariaDB database driver
*/
MARIADB("org.mariadb.jdbc.Driver", "org.mariadb.jdbc.MariaDbDataSource", "jdbc:mariadb:"),
/**
* PostgreSQL database driver
*/
POSTGRESQL("org.postgresql.Driver", "org.postgresql.xa.PGXADataSource", "jdbc:postgresql:"),
/**
* Oracle database driver
*/
ORACLE("oracle.jdbc.OracleDriver", "oracle.jdbc.xa.client.OracleXADataSource", "jdbc:oracle:"),
/**
* Microsoft SQL Server database driver
*/
SQLSERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"com.microsoft.sqlserver.jdbc.SQLServerXADataSource", "jdbc:sqlserver:");
private final String driverClassName;
private final String xaDataSourceClassName;
private final String urlPrefix;
/**
* Return the driver class name
* @return the driver class name
*/
public String getDriverClassName();
/**
* Return the XA DataSource class name
* @return the XA DataSource class name
*/
public String getXaDataSourceClassName();
/**
* Return the URL prefix for this driver
* @return the URL prefix
*/
public String getUrlPrefix();
/**
* Find a DatabaseDriver for the given URL
* @param url the JDBC URL
* @return the database driver or null
*/
public static DatabaseDriver fromJdbcUrl(String url);
/**
* Find a DatabaseDriver for the given driver class name
* @param driverClassName the driver class name
* @return the database driver or null
*/
public static DatabaseDriver fromDriverClassName(String driverClassName);
}
/**
* Connection details for embedded databases
*/
public enum EmbeddedDatabaseConnection {
/**
* No embedded database
*/
NONE(null, null, null),
/**
* H2 embedded database connection
*/
H2(DatabaseDriver.H2, "org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration$H2EmbeddedConfiguration",
"jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"),
/**
* Derby embedded database connection
*/
DERBY(DatabaseDriver.DERBY, "org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration$DerbyEmbeddedConfiguration",
"jdbc:derby:memory:%s;create=true"),
/**
* HSQL embedded database connection
*/
HSQLDB(DatabaseDriver.HSQLDB, "org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration$HsqldbEmbeddedConfiguration",
"jdbc:hsqldb:mem:%s");
private final DatabaseDriver databaseDriver;
private final String type;
private final String url;
/**
* Returns the driver class for the embedded database
* @return the driver class
*/
public String getDriverClassName();
/**
* Returns the URL for the embedded database
* @param databaseName the database name
* @return the database URL
*/
public String getUrl(String databaseName);
/**
* Convenience method to determine if a driver class corresponds to this connection
* @param driverClass the driver class
* @return true if the driver class matches
*/
public boolean isDriverClass(String driverClass);
/**
* Get the EmbeddedDatabaseConnection for the given class loader
* @param classLoader the class loader used to check for classes
* @return the embedded connection
*/
public static EmbeddedDatabaseConnection get(ClassLoader classLoader);
}Usage Examples:
// Basic DataSource configuration
@Configuration
public class DatabaseConfig {
@Bean
@ConfigurationProperties(prefix = "app.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create()
.type(HikariDataSource.class)
.build();
}
}
// Multiple DataSource configuration
@Configuration
public class MultipleDataSourceConfig {
@Bean
@Primary
@ConfigurationProperties(prefix = "app.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "app.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}Java Persistence API and Object-Relational Mapping support.
/**
* Configures the base packages used by auto-configuration when scanning for entity classes
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface EntityScan {
/**
* Alias for basePackages()
* @return the base packages to scan
*/
String[] value() default {};
/**
* Base packages to scan for entities
* @return the base packages to scan
*/
String[] basePackages() default {};
/**
* Type-safe alternative to basePackages() for specifying packages to scan for entities
* @return the base package classes
*/
Class<?>[] basePackageClasses() default {};
}
/**
* Callback interface that can be used to customize Hibernate properties
*/
@FunctionalInterface
public interface HibernatePropertiesCustomizer {
/**
* Customize the specified Hibernate properties
* @param hibernateProperties the hibernate properties to customize
*/
void customize(Map<String, Object> hibernateProperties);
}
/**
* Callback interface that can be used to customize JPA properties
*/
@FunctionalInterface
public interface JpaPropertiesCustomizer {
/**
* Customize the specified JPA properties
* @param jpaProperties the JPA properties to customize
*/
void customize(Map<String, Object> jpaProperties);
}Usage Examples:
// Entity scanning configuration
@SpringBootApplication
@EntityScan(basePackages = {"com.example.entities", "com.example.domain"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
// Custom Hibernate properties
@Configuration
public class HibernateConfig {
@Bean
public HibernatePropertiesCustomizer hibernatePropertiesCustomizer() {
return properties -> {
properties.put("hibernate.show_sql", true);
properties.put("hibernate.format_sql", true);
properties.put("hibernate.use_sql_comments", true);
properties.put("hibernate.jdbc.batch_size", 20);
};
}
}
// JPA repository configuration
@EnableJpaRepositories(basePackages = "com.example.repositories")
@Configuration
public class JpaConfig {
@Bean
public JpaPropertiesCustomizer jpaPropertiesCustomizer() {
return properties -> {
properties.put("javax.persistence.validation.mode", "callback");
properties.put("javax.persistence.schema-generation.database.action", "validate");
};
}
}Reactive Relational Database Connectivity for non-blocking database access.
/**
* Builder for creating R2DBC ConnectionFactory instances
*/
public final class ConnectionFactoryBuilder {
/**
* Create a new ConnectionFactoryBuilder
* @return a new connection factory builder
*/
public static ConnectionFactoryBuilder create();
/**
* Configure the connection factory URL
* @param url the connection URL
* @return the builder
*/
public ConnectionFactoryBuilder url(String url);
/**
* Configure the driver class name
* @param driver the driver class name
* @return the builder
*/
public ConnectionFactoryBuilder driver(String driver);
/**
* Configure the connection host
* @param host the host
* @return the builder
*/
public ConnectionFactoryBuilder host(String host);
/**
* Configure the connection port
* @param port the port
* @return the builder
*/
public ConnectionFactoryBuilder port(int port);
/**
* Configure the database name
* @param database the database name
* @return the builder
*/
public ConnectionFactoryBuilder database(String database);
/**
* Configure the username
* @param username the username
* @return the builder
*/
public ConnectionFactoryBuilder username(String username);
/**
* Configure the password
* @param password the password
* @return the builder
*/
public ConnectionFactoryBuilder password(String password);
/**
* Configure an option
* @param option the option name
* @param value the option value
* @return the builder
*/
public ConnectionFactoryBuilder option(String option, Object value);
/**
* Build the ConnectionFactory
* @return the connection factory
*/
public ConnectionFactory build();
}
/**
* Script-based DataBaseInitializer for R2DBC
*/
public class R2dbcScriptDatabaseInitializer extends AbstractScriptDatabaseInitializer {
/**
* Create a new R2dbcScriptDatabaseInitializer instance
* @param connectionFactory the connection factory to use
* @param settings the initialization settings
*/
public R2dbcScriptDatabaseInitializer(ConnectionFactory connectionFactory, DatabaseInitializationSettings settings);
@Override
protected boolean supportsDatabase(DatabaseDriver driver);
}Usage Examples:
// R2DBC ConnectionFactory configuration
@Configuration
@EnableR2dbcRepositories
public class R2dbcConfig {
@Bean
@ConfigurationProperties(prefix = "app.r2dbc")
public ConnectionFactory connectionFactory() {
return ConnectionFactoryBuilder.create()
.driver("postgresql")
.host("localhost")
.port(5432)
.database("testdb")
.username("user")
.password("password")
.build();
}
@Bean
public R2dbcTransactionManager transactionManager(ConnectionFactory connectionFactory) {
return new R2dbcTransactionManager(connectionFactory);
}
}Database migration tools integration for Flyway and Liquibase.
/**
* Strategy interface used to determine how Flyway migration should be applied
*/
@FunctionalInterface
public interface FlywayMigrationStrategy {
/**
* Trigger flyway migration
* @param flyway the flyway instance
*/
void migrate(Flyway flyway);
}
/**
* Strategy interface used to determine how Liquibase migration should be applied
*/
@FunctionalInterface
public interface LiquibaseMigrationStrategy {
/**
* Trigger liquibase migration
* @param liquibase the liquibase instance
*/
void migrate(Liquibase liquibase);
}
/**
* Callback interface that can be used to customize Flyway configuration
*/
@FunctionalInterface
public interface FlywayConfigurationCustomizer {
/**
* Customize the given Flyway configuration
* @param configuration the configuration to customize
*/
void customize(FluentConfiguration configuration);
}Usage Examples:
// Custom Flyway migration strategy
@Configuration
public class MigrationConfig {
@Bean
public FlywayMigrationStrategy flywayMigrationStrategy() {
return flyway -> {
// Clean the database first (only for development!)
if (isDevelopmentEnvironment()) {
flyway.clean();
}
flyway.migrate();
};
}
@Bean
public FlywayConfigurationCustomizer flywayConfigurationCustomizer() {
return configuration -> {
configuration
.locations("classpath:db/migration", "classpath:db/data")
.placeholders(Map.of("engine", "InnoDB"))
.validateOnMigrate(true);
};
}
}
// Custom Liquibase migration strategy
@Configuration
public class LiquibaseConfig {
@Bean
public LiquibaseMigrationStrategy liquibaseMigrationStrategy() {
return liquibase -> {
try {
liquibase.update("production");
} catch (Exception e) {
throw new RuntimeException("Failed to migrate database", e);
}
};
}
}Database initialization support for SQL scripts and schema creation.
/**
* Settings for initializing a SQL database
*/
public class DatabaseInitializationSettings {
/**
* Create new database initialization settings
*/
public DatabaseInitializationSettings();
/**
* Set the schema locations
* @param schemaLocations the schema locations
*/
public void setSchemaLocations(List<String> schemaLocations);
/**
* Get the schema locations
* @return the schema locations
*/
public List<String> getSchemaLocations();
/**
* Set the data locations
* @param dataLocations the data locations
*/
public void setDataLocations(List<String> dataLocations);
/**
* Get the data locations
* @return the data locations
*/
public List<String> getDataLocations();
/**
* Set whether to continue on error
* @param continueOnError whether to continue on error
*/
public void setContinueOnError(boolean continueOnError);
/**
* Get whether to continue on error
* @return whether to continue on error
*/
public boolean isContinueOnError();
/**
* Set the statement separator
* @param separator the statement separator
*/
public void setSeparator(String separator);
/**
* Get the statement separator
* @return the statement separator
*/
public String getSeparator();
/**
* Set the SQL script encoding
* @param encoding the encoding
*/
public void setEncoding(Charset encoding);
/**
* Get the SQL script encoding
* @return the encoding
*/
public Charset getEncoding();
}
/**
* Base class for script-based database initializers
*/
public abstract class AbstractScriptDatabaseInitializer implements DatabaseInitializer {
/**
* Create a new AbstractScriptDatabaseInitializer
* @param settings the initialization settings
*/
protected AbstractScriptDatabaseInitializer(DatabaseInitializationSettings settings);
@Override
public boolean initializeDatabase();
/**
* Returns whether the given database driver is supported
* @param driver the database driver
* @return true if supported
*/
protected abstract boolean supportsDatabase(DatabaseDriver driver);
}Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-boot--spring-boot-starter