Automatically configure springboot
The automatic configuration function will scan all META-INF/ files under referenced jar packages.
Find the corresponding data
As an automatic configuration class, then determine whether to load them based on the conditions
Automatic configuration with DataSource
The automatic configuration class for the DataSource data source is DataSourceAutoConfiguration:
@Configuration(proxyBeanMethods = false) @ConditionalOnClass({ , }) @ConditionalOnMissingBean(type = "io.") @AutoConfigureBefore() @EnableConfigurationProperties() @Import({ , , }) public class DataSourceAutoConfiguration {}
1. @ConditionalOnClass({ , }) means that the two classes DataSource EmbeddedDatabaseType must exist in the project, and EmbeddedDatabaseType is in the spring-boot-starter-jdbc dependency package, so it needs to be introduced.
2. Driver packages are required to connect to the database, such as: mysql-connector-java
3.@EnableConfigurationProperties() means that the DataSourceProperties configuration class is enabled, and the database configuration information is in this configuration class.
The default data source is HikariDataSource for the following reasons:
- 4.1 After DataSourceAutoConfiguration is enabled, there is an internal configuration class:
@Configuration(proxyBeanMethods = false) @Conditional() @ConditionalOnMissingBean({ , }) @Import({ , , DataSourceConfiguration., , , }) protected static class PooledDataSourceConfiguration {}
By default, the above conditions will be met. You need to pay attention to the @Import annotation. Many configuration classes are imported here, and there will also be various @Conditional... Conditions displayed. By default, only the configuration class conditions will be met, as follows:
@Configuration(proxyBeanMethods = false) @ConditionalOnClass() @ConditionalOnMissingBean() @ConditionalOnProperty(name = "", havingValue = "",matchIfMissing = true) static class Hikari { @Bean @ConfigurationProperties(prefix = "") HikariDataSource dataSource(DataSourceProperties properties) { HikariDataSource dataSource = createDataSource(properties, ); if ((())) { (()); } return dataSource; } }
You can see that you can set the data source type to be used by setting. Here matchIfMissing = true is the key to HikariDataSource becoming the default data source. It means that if it is not configured, this condition will still be met.
When loading the HikariDataSource bean internally, you can see that the configuration file is marked with the prefix. In fact, the DataSourceProperties parameter here already contains the database information configured with the prefix, and this information will be passed to the HikariDataSource data source, and the configuration information using the prefix will also be applied to the HikariDataSource. If there are duplications between the two, the configured data will overwrite the previous configuration.
Summarize
The above is the logic of springboot configuration data source and HikariDataSource as the default data source. Springboot uses configuration, which controls that only one DataSource instance will be created and loaded.
These are just personal experience. I hope you can give me a reference and I hope you can support me more.