How does autoconfig work
It adds flexibility to the developer to register beans based on several conditions like:. We can have an identical implementation for the Canadian tax service.
If we run our application, Spring will ensure to load the correct bean based on country Locale in our example. Internally auto-configuration is achieved with Configuration annotation. Spring Boot, use the Conditional annotations to determine if it require setting up auto-configuration for the configuration or not. Usually auto-configuration classes use the following conditional annotations:. Out of the box, Spring provides plenty of conditional annotations. Please consult reference documentation.
The class conditions are the most common conditional annotation use by auto-configuration for the detection. The ConditionalOnClass annotation allows it to include the configuration class based on a specific class.
Here is our JacksonAutoConfiguration configuration class:. The condition is true if the DataSource class is not on the classpath. The condition is true if the application is not a web application. ConditionalOnProperty "my. The condition is true if my.
ConditionalOnResource "classpath:my. Matches if there is exactly one primary DataSource bean specified in your application. The condition is true if the application is a web application. So, in short, with Spring Boot you do not have to write the most common conditions yourself like checking for a property. Instead, you can use its enhanced Conditional annotations. It also reads in the spring. It has an enhanced concept of Conditionals, compared to plain Spring.
This will come in handy in a second. To follow along, you might want to checkout the Spring Boot project yourself. Also note, that Spring Boot effectively switched to Gradle as build system in the master branch, whereas older release branches are still Maven based. It is the one that builds a DataSource for you, after you put a couple of properties like "spring.
For the Configuration to get evaluated further, you need to have two classes on the classpath: DataSource and EmbeddedDatabaseType. If that Conditional is false, the whole Configuration is not evaluated. We can ignore these two lines for now, but as a quick side-note: EnableConfigurationProperties enables that the properties you put into your.
One of them is the PooledDataSourceConfiguration, which will conditionally create a connection pool DataSource for you. The PooledDataSourceConfiguration only gets evaluated further if the user i.
The PooledDataSourceConfiguration imports quite a few other configurations, to be more specific: one configuration for each supported connection pool library Hikari, Tomcat, Dbcp2, etc.
That is a lot to digest, but essentially it is normal Spring Configurations, safe-guarded with Conditionals. The HikariDataSource. Either the property "spring. If all these conditions match, then a good old Spring Bean gets created. A HikariDataSource.
So, essentially all the DataSourceAutoConfiguration does is check for 3rd party dependencies on the classpath and a couple of properties to be set. Then it boots up a DataSource for you that you would otherwise have to configure yourself, manually. Simply by switching dependencies. Back to the question from the beginning: How can Spring Boot boot up an embedded Tomcat server by default?
The exercise for you is to find and understand those servlet specific auto-configurations, with their appropriate conditionals. And if you are done with that, you can have a look at the AutoConfiguration for one of your favorite libraries, like Flyway, or Jackson, or MongoDB.
Checking for dependencies or rather specific classes of dependencies think: Tomcat, HikariCP, etc. If the user specified his own, e. One last piece of the puzzle is missing. How do all these dependencies get on the classpath in Spring Boot projects? And how come you do not have to specify any version numbers for your 3rd party libraries? The one dependency every web-based Spring Boot project includes, is the spring-boot-starter-web dependency.
Its pom. Among other dependencies, the spring-boot-starter-tomcat dependency gets pulled in transitively by the starter-web dependency. Spring Boot pulls it in, and guess what that means.
That by default, ConditionalOnClass Tomcat. And that is exactly the condition Spring Boot evaluates to start up an embedded Tomcat server. Not so much rocket science, anymore, is it?
The simple configuration for current app look like this —. Then, you have to configure data source explicitly as below —. Lots of work done here. Spring boot takes care of these configurations for you. I am using spring boot 2. The autoconfigure jar has many packages as below —. Apart from this, Another important file inside spring-boot-autoconfigure. This file lists all the spring boot auto-configuration classes that should be enabled under the EnableAutoConfiguration key.
If you are using 2. Spring Boot 1. The discovery algorithm is described in 2. In order to connect to the database, we need to use JPA starter project in pom. The dependency looks like this —. As soon as you include spring-boot-starter-data-jpa into your pom. Consequently, HikariCP library is present in app classpath at run time.
So the class com. HikariDataSource included in the classpath. The auto-configuration logic is written in class DataSourceAutoConfiguration. Here, the DataSource class is present in the classpath. Can you guess, how? Because of the HikariCP library. This line enables binding the properties defined in application.
To accomplish that we can create Conditions to check the presence or absence of MongoDB driver class "com. Server" as follows:. We just have seen how to register beans conditionally based on the presence or absence of a class in classpath. We can create a Condition to check if there is any existing bean of a certain type as follows:.
We have just seen how to implement various types of Conditions. But there is even more elegant way to implement Conditions using Annotations. Then we can implement DatabaseTypeCondition to use the DatabaseType value to determine whether to enable or disable bean registration as follows:. Now we can use the DatabaseType annotation on our bean definitions as follows:. Here we are getting the metadata from DatabaseType annotation and checking against the System Property dbType value to determine whether to enable or disable the bean registration.
We have seen good number of examples to understand how we can register beans conditionally using Conditional annotation. Spring Boot extensively uses Conditional feature to register beans conditionally based on various criteria.
You can find various Condition implementations that SpringBoot uses in org. Now that we've come to know about how Spring Boot uses the Conditional feature to conditionally check whether to register a bean or not, but what exactly triggers the auto-configuration mechanism? Typically we annotate our Application entry point class with either SpringBootApplication or if we want to customize the defaults we can use the following annotations:.
0コメント