SpringBoot data source - why the default thread pool is usually Hikari

This article mainly explains the DataSourceAutoConfiguration class, and then explains the annotations of this class, so open this class directly and start watching...

ps: The version I use is 2.3.0.RELEASE

Table of contents

DataSourceAutoConfiguration class annotation

@Configuration(proxyBeanMethods = false)

@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })

@ConditionalOnMissingBean(type = "io.r2dbc.spi.ConnectionFactory")

@EnableConfigurationProperties(DataSourceProperties.class)

@Import({ DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class })

DataSourcePoolMetadataProvidersConfiguration

DataSourceInitializationConfiguration

Internal class EmbeddedDatabaseConfiguration annotation

@Conditional(EmbeddedDatabaseCondition.class)

Internal class PooledDataSourceConfiguration annotation

@Conditional(PooledDataSourceCondition.class)

Summarize


DataSourceAutoConfiguration class annotation

@Configuration(proxyBeanMethods = false)

The @Configuration annotation is more commonly used, so I won’t talk about it, mainly because proxyBeanMethods = false. Indicates that the proxy mode is disabled. At this time, Spring will not generate a proxy object for the class marked by @Configurationthe annotation , but directly call the method in it.

In actual development, if the methods in a class are all independent, stateless bean configuration methods (that is, methods defined by @Beanannotations ), you can @Configuration(proxyBeanMethods = false)apply to the class to improve performance. Because disabling the proxy mode can avoid Spring creating a proxy object for each Bean configuration method, thereby reducing the overhead of Spring container initialization and runtime. However, it should be noted that disabling the proxy mode may cause some functions to fail, such as the declared Bean cannot be enhanced by the aspect.

@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })

Indicates that the Bean or configuration class annotated by this annotation will be created and registered only when DataSourceand these two classes exist on the classpath of the current application. EmbeddedDatabaseTypeAmong them DataSourceis the database connection pool interface in the Java standard library, and EmbeddedDatabaseTypeis an enumeration class provided by Spring Boot to support embedded databases.

@ConditionalOnMissingBean(type = "io.r2dbc.spi.ConnectionFactory")

Indicates that the bean or configuration class marked by this annotation will be created and registered only when there is no bean io.r2dbc.spi.ConnectionFactoryof the type . where io.r2dbc.spi.ConnectionFactoryis the R2DBC database connection abstract interface.

@EnableConfigurationProperties(DataSourceProperties.class)

Indicates that automatic injection of DataSourcePropertiesconfiguration . Spring Boot will setterautomatically match DataSourcePropertiesthe attributes in according to the name of the field or method, and automatically inject the attribute values ​​in the configuration file into DataSourcePropertiesthe class .

Usually, when using Spring Boot to develop applications, we use @ConfigurationPropertiesannotations to define the configuration items required by the application. By defining a configuration attribute class corresponding to the configuration file and using @EnableConfigurationPropertiesto enable automatic injection, we can automatically inject the attribute values ​​in the configuration file into the configuration attribute class, and then use the configuration attribute class object in the application, thus conveniently Get and manage application configuration information.

@Import({ DataSourcePoolMetadataProvidersConfiguration.class, DataSourceInitializationConfiguration.class })

In @Importthe annotation , an array containing multiple configuration classes is passed in, which means that the beans or components defined in these configuration classes are imported into the current configuration class, so that they can be registered in the Spring container together when the container starts.

Specifically, when using Spring Boot to develop applications, we may need to use beans or components in other modules or third-party libraries. At this time, we can import related configuration classes or components into the configuration class of the current application through @Importannotations

DataSourcePoolMetadataProvidersConfiguration

Provide a DataSourcePoolMetadataProvider instance for supported data sources, which is used to provide data source connection pool metadata information, including connection pool name, maximum number of connections, number of idle connections, etc.

DataSourceInitializationConfiguration

Initialize the data source, including executing SQL scripts, setting data source encoding, etc.

Internal class EmbeddedDatabaseConfiguration annotation

@Configuration(proxyBeanMethods = false) will not go into details.

@Conditional(EmbeddedDatabaseCondition.class)

If the conditions of the EmbeddedDatabaseCondition are met, the annotation can be continued to read

The judgment condition is the code of getMatchOutcome

It can be seen that if spring.datasource.url is set, it does not meet the conditions

If the pooledCondition is met, the condition is not met (see PooledDataSourceCondition directly, and we will talk about it below )

If there is no EmbeddedDatabase, the condition is not met, and the rest are met

So the embedded data source will not be used in the above cases

Notes below will not repeat

Internal class PooledDataSourceConfiguration annotation

@Configuration(proxyBeanMethods = false) no longer repeat

@Conditional(PooledDataSourceCondition.class)

This condition class inherits the AnyNestedCondition class, which means that any condition will take effect

Specify spring.datasource.type to take effect

"com.zaxxer.hikari.HikariDataSource",
"org.apache.tomcat.jdbc.pool.DataSource", 
"org.apache.commons.dbcp2.BasicDataSource"

If one of the above three data sources is available, it will take effect, and the built-in HikariDataSource will take effect @ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) will not repeat it

The last Import annotation is the first to import DataSourceConfiguration.Hikari, so it is Hikari that takes effect, and there is a @ConditionalOnMissingBean(DataSource.class) on the annotations of these classes.

Summarize

Data source appetizer, review annotations

Guess you like

Origin blog.csdn.net/wai_58934/article/details/130810609