Autoconfiguration

As mentioned earlier, the autoconfiguration ability of Spring Boot starters comes from spring-boot-autoconfigure. This project is the autoconfiguration implementation of Spring Boot's official starters.

The @SpringBootApplication annotation is the trigger for the autoconfiguration. This annotation is a combination of these three annotations:

  • @SpringBootConfiguration
  • @EnableAutoConfiguration
  • @ComponentScan

The @SpringBootConfiguration annotation indicates that a class is a Spring Boot application configuration class. The @EnableAutoConfiguration annotation is the one that actually enables autoconfiguration, as its name suggests. The @ComponentScan annotation is to enable Spring's component scan feature.

With the @EnableAutoConfiguration annotation, spring-boot-autoconfigure will load metadata from the META-INF/spring.factories resource file using the application's class loader. And all of the spring.factories files of the application's dependencies will be loaded, including official starters as well as custom starters. Inside the spring.factories metadata file, the autoconfiguration is hooked through the org.springframework.boot.autoconfigure.EnableAutoConfiguration property.

Let's take a look at the spring.factories file of spring-boot-autoconfigure, which contains the autoconfiguration hooks of all the official starters.

The following is a snippet of the official spring.factories file:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
...
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,
...
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,
...

As you can see, this metadata file has the same format as a .properties file, which is a list of key-value pairs. The value of the org.springframework.boot.autoconfigure.EnableAutoConfiguration key is a list of names of the autoconfiguration classes, separated by commas. AopAutoConfiguration and DataSourceAutoConfiguration are the two that we have used in the Messages App. An autoconfiguration class is also a Java class annotated with Spring's @Configuration annotation.

Inside these autoconfiguration classes, Spring will create beans as configured. The only special part of these classes is that their configurations are conditional. For example, if you have already defined a DataSource bean in your application's configuration class, Spring Boot will not create another DataSource bean. In our Messages App, we let Spring Boot do autoconfiguration for us by leaving the definition of a DataSource bean empty. This is called conditional configuration. To archive conditional configuration, Spring provides annotations such as @Conditional, @ConditionalOnClass, @ConditionalOnMissingBean, and other conditional annotations that you can use to specify under what conditions autoconfiguration should be applied. Take the DataSourceAutoConfiguration class as an example; autoconfiguration will only happen when there are DataSource classes available in the classpath. The connection pool HiKari will be initialized when there is no user-defined bean of the DataSource type in the Spring container.

We're not going to go into any more detail on the usage of conditional autoconfiguration. If you are interested, you can check the source code for DataSourceAutoConfiguration here: https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java.

Custom starters follow the same autoconfiguration mechanism as the official starters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.228.88