Spring boot implements auto-load datasource and related configurations. Of course, it is necessary to add @enableautoconfiguration annotations when using them. Here is the source of this part of the analysis.
(1) org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration is called when Spring boot is started. Here are some of the source code.
1 @Configuration2@ConditionalOnClass ({DataSource.class, Embeddeddatabasetype.class })3@EnableConfigurationProperties (datasourceproperties.class)4@Import ({datasourcepoolmetadataprovidersconfiguration.class,5Datasourceinitializationconfiguration.class })6 Public classdatasourceautoconfiguration {7 8 @Configuration9@Conditional (embeddeddatabasecondition.class)Ten@ConditionalOnMissingBean ({DataSource.class, Xadatasource.class }) One@Import (embeddeddatasourceconfiguration.class) A protected Static classembeddeddatabaseconfiguration { - } - the @Configuration -@Conditional (pooleddatasourcecondition.class) -@ConditionalOnMissingBean ({DataSource.class, Xadatasource.class }) -@Import ({Datasourceconfiguration.hikari.class, Datasourceconfiguration.tomcat.class, +Datasourceconfiguration.dbcp2.class, Datasourceconfiguration.generic.class, -Datasourcejmxconfiguration.class }) + protected Static classpooleddatasourceconfiguration { A } at ...... -}
We can see from this that there are two nested classes in Datasourceautoconfiguration, one is embeddeddatabaseconfiguration and the other is pooleddatasourceconfiguration.
Embeddeddatabaseconfiguration indicates that the DataSource, which has been embedded in spring boot, can be used without additional configuration, except for Maven to add the corresponding driver. As you can see from the Embeddeddatabasetype class, the inline datasource of Spring boot supports hsql,h2,derby of these three kinds of db.
Pooleddatasourceconfiguration says spring boot also supports some datasource that implement the pool. As you can see from the Org.springframework.boot.jdbc.DataSourceBuilder, the current version of spring Boot (2.0) only supports Com.zaxxer.hikari.hikaridatasource,org.apache.tomcat.jdbc.pool.datasource,org.apache.commons.dbcp2.basicdatas Ource. Hikaridatasource is the default choice for spring boot (datasourcebuilder in data_source_type_names[0]= Com.zaxxer.hikari.HikariDataSource). Therefore, when the following configuration is done in the Application.yml file, Spring boot uses the Hikaridatasource database connection pool by default.
Spring: datasource: URL: Jdbc:mysql://localhost:3306/sas username : Root Password: * * * driver-class-name: com.mysql.jdbc.Driver #type : Com.zaxxer.hikari.HikariDataSource
(2) We take hikaridatasource for example, Next, call the Hikari nested class of the Org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration abstract class in Pooleddatasourceconfiguration (DataS Ourceconfiguration an implementation class for an abstract class).
1 Abstract classdatasourceconfiguration {2 3@SuppressWarnings ("Unchecked")4 protected<T>T CreateDataSource (datasourceproperties properties,5class<?extendsDatasource>type) {6 return(T) Properties.initializedatasourcebuilder (). Type (type). Build ();7 }8 9 /*Omit Tomcat Pool DataSource configuration.*/Ten /** One * Hikari DataSource configuration. A */ -@ConditionalOnClass (Hikaridatasource.class) -@ConditionalOnProperty (name = "Spring.datasource.type", Havingvalue = "Com.zaxxer.hikari.HikariDataSource", matchifmissing =true) the Static classHikariextendsdatasourceconfiguration { - - @Bean -@ConfigurationProperties (prefix = "Spring.datasource.hikari") + Publichikaridatasource DataSource (datasourceproperties properties) { -Hikaridatasource DataSource =CreateDataSource (Properties, +Hikaridatasource.class); A if(Stringutils.hastext (Properties.getname ())) { at Datasource.setpoolname (Properties.getname ()); - } - returnDataSource; - } - } - /*Omit DBCP DataSource configuration.*/ in}
We can see from the yellow section that when the Application.yml file is configured spring.datasource.type= Com.zaxxer.hikari.HikariDataSource, Hikaridatasource is used as the database connection pool. It will
Spring Boot (3): Source analysis of loading DataSource process and configuration of DataSource in Yml