application-database.properties
#初始化数据库的时候,如果错误,是否继续启动。spring.datasource.continue-on-error=false#jdbc driver.默认通过uri来自动检测。spring.datasource.driver-class-name=com.mysql.jdbc.Driver#使用的db名称spring.datasource.name=test#是否随机生成db名称spring.datasource.generate-unique-name=false#jdbc url.连接数据库的urispring.datasource.url=#数据库连接用户名spring.datasource.username=#数据连接密码spring.datasource.password=#DML的用户名(如果数据库专门设置了对应的用户名和密码)spring.datasource.data-username=#DML的密码(如果数据库专门设置了对应的用户名和密码)spring.datasource.data-password=#DDL的用户名(如果数据库专门设置了对应的用户名和密码)spring.datasource.schema-username=#DDL的密码(如果数据库专门设置了对应的用户名和密码)spring.datasource.schema-password=#全限定名,连接池。默认自动检测classpathspring.datasource.type=com.zaxxer.hikari.HikariDataSource#sql脚本字符spring.datasource.sql-script-encoding=UTF-8#sql脚本分割符,默认为分号。spring.datasource.separator=;#dbcp2的连接参数spring.datasource.dbcp2.*#hikari的连接参数spring.datasource.hikari.*#tomcat的连接参数spring.datasource.tomcat.*
SOURCE-datasourceproperties
Load the corresponding Spring.datasource configuration parameters.
@ConfigurationProperties (prefix = "Spring.datasource") public class Datasourceproperties implements Beanclassloaderaware, Initializingbean {private ClassLoader ClassLoader; /** * Name of the DataSource. The Default to "TestDB" is when using a embedded database. */private String name; /** * Whether to generate a random datasource name. */Private Boolean generateuniquename; /** * Fully qualified name of the connection pool implementation to use. By default, it's auto-detected from the classpath. */private class<? extends datasource> type; /** * Fully qualified name of the JDBC driver. Auto-detected based on the URL by default. */Private String driverclassname; /** * JDBC URL of the database. */private String URL; /** * Login username of the database. */private String username; /** * Login password of the database. */private String password; /** * JNDI Location of theDataSource. Class, URL, username & password is ignored when * set. */Private String jndiname; /** * Initialize the DataSource with available DDL and DML scripts. */private Datasourceinitializationmode initializationmode = datasourceinitializationmode.embedded; /** * Platform to use in the DDL or DML scripts (such as Schema-${platform}.sql or * data-${platform}.sql). */private String platform = "All"; /** * Schema (DDL) script resource references. */private list<string> schema; /** * Username of the database to execute DDL scripts (if different). */Private String schemausername; /** * Password of the database to execute DDL scripts (if different). */Private String Schemapassword; /** * Data (DML) script resource references. */private list<string> data; /** * Username of the database to execute DML scripts (if different). */Private String datausername; /** * Password of the database to execute DML scripts (if different). */Private String Datapassword; /** * Whether to stop if a error occurs while initializing the database. */Private Boolean continueonerror = false; /** * Statement Separator in SQL initialization scripts. */Private String separator = ";"; /** * SQL scripts encoding. */Private Charset sqlscriptencoding; Private Embeddeddatabaseconnection embeddeddatabaseconnection = Embeddeddatabaseconnection.none; Private xa xa = new xa (); Private String UniqueName;
Source-datasourceautoconfiguration
automatically assembles datasource. According to the datasourceproperties parameter.
@Configuration @conditionalonclass ({datasource.class, embeddeddatabasetype.class}) @EnableConfigurationProperties (Datasourceproperties.class) @Import ({datasourcepoolmetadataprovidersconfiguration.class, Datasourceinitializationconfiguration.class}) public class Datasourceautoconfiguration {@Configuration @Conditional (Embeddeddatabasecondition.class) @ConditionalOnMissingBean ({datasource.class, xadatasource.class}) @Import (Embed Deddatasourceconfiguration.class) protected static class Embeddeddatabaseconfiguration {} @Configuration @Con Ditional (Pooleddatasourcecondition.class) @ConditionalOnMissingBean ({datasource.class, xadatasource.class}) @Impo RT ({DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class, Datasourceconfiguration.dbcp2. Class, DataSourceConfiguration.Generic.class, Datasourcejmxconfiguration.class}) protected static class Poo leddatasourceconfiguration {}/** * {@link anynesTedcondition} that checks this either {@code Spring.datasource.type} * is set or {@link Pooleddatasourceavailablecondi tion} applies. */Static Class Pooleddatasourcecondition extends Anynestedcondition {pooleddatasourcecondition () { Super (Configurationphase.parse_configuration); } @ConditionalOnProperty (prefix = "Spring.datasource", name = "type") static class Explicittype {} @Conditional (pooleddatasourceavailablecondition.class) static class Pooleddatasourceavailable {}} /** * {@link Condition} to test if a supported connection pool is available. */Static Class Pooleddatasourceavailablecondition extends Springbootcondition {@Override public conditi Onoutcome Getmatchoutcome (conditioncontext context, Annotatedtypemetadata metadata) {Conditionm Essage. Builder message = conditionmessage. Forcondition ("Pooleddatasource"); if (getdatasourceclassloader (context) = null) {return conditionoutcome. Match (Mes sage.foundexactly ("Supported DataSource")); } return Conditionoutcome. NoMatch (Message.didnotfind ("Supported DataSource"). Atall ()); }/** * Returns the class loader for the {@link DataSource} class. Used to ensure that * the driver class can actually is loaded by the data source. * @param context The condition context * @return the class loader */Private ClassLoader Getdatasou Rceclassloader (Conditioncontext context) {class<?> Datasourceclass = Datasourcebuilder . Findtype (Context.getclassloader ()); return (Datasourceclass = = null? Null:dataSourceClass.getClassLoader ()); }}/** * {@link Condition} to detect if an embedded {@link DataSource} type can be used. * If a pooled {@link DataSource} is Available, it always is preferred to an * {@code embeddeddatabase}. */Static Class Embeddeddatabasecondition extends Springbootcondition {private final springbootcondition pooled Condition = new Pooleddatasourcecondition (); @Override public Conditionoutcome Getmatchoutcome (conditioncontext context, annotatedtypemetadata me Tadata) {Conditionmessage.builder message = Conditionmessage. Forcondition ("Embeddeddatasou Rce "); if (anymatches (context, metadata, this.pooledcondition)) {return conditionoutcome. NoMatch (message.foundexactly ("Supported pooled data source"); } embeddeddatabasetype type = Embeddeddatabaseconnection. Get (Context.getclassloader ()). Gett Ype (); if (type = = null) {return conditionoutcome. NoMatch (Message.didnotfind ("Embedded da Tabase "). Atall ()); } return Conditionoutcome.match (Message.found ("Embedded Database"). Items (type)); } }}
Test
@RunWith(SpringRunner.class)@SpringBootTestpublic class AppContextTest { @Qualifier("dataSource") @Autowired private DataSource dataSource; @Test public void dataSourceTest() throws SQLException { Connection connection = null; try { connection = dataSource.getConnection(); int transactionIsolation = connection.getTransactionIsolation(); String schema = connection.getSchema(); System.out.println("transactionIsolation="+transactionIsolation+";schema="+schema); } catch (SQLException e) { e.printStackTrace(); }finally { if (null != connection) connection.close(); } }
Spring boot uses the Hikaridatasource database connection pool by default.
Druid Database connection pool: http://blog.51cto.com/881206524/2121687
Spring Boot DataSource