SpringBoot AutoConfigure learning summary

  1. Advantages of SpringBoot

Auto-configuration is the biggest highlight of Spring Boot, which perfectly demonstrates CoC conventions due to configuration. Spring Boot can automatically configure various Spring sub-projects (Spring MVC, Spring Security, Spring Data, Spring Cloud, Spring Integration, Spring Batch, etc.) and various beans that need to be defined by third-party open source frameworks.

Spring Boot defines various XxxxAutoConfiguration configuration classes internally, and pre-defined various required beans. These configuration classes will only be invoked under certain circumstances.
2. Automatic configuration class list
spring-boot-autoconfigure-1.5.1.RELEASE.jar/META-INF/spring.factories

Auto Configure

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
……
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration,\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\
org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration

  1. Auto-configuration class definitions

Take org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration as an example:

  1. @Configuration
  2. @ConditionalOnWebApplication(type = Type.SERVLET)
  3. @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurerAdapter.class })
  4. @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
  5. @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
  6. @AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class })
  7. public class WebMvcAutoConfiguration {

    1. @Bean
  8. @ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
  9. public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
  10. return new OrderedHiddenHttpMethodFilter();
  11. }

    1. // …

    1. }

  1. How to load these auto-configured classes

By executing the SpringApplication.run() method, the current SpringBootDemoApplication will be passed in as the source, and the SpringApplication class will also read the settings in spring.factories and build the Context of the application

  1. Conditions for automatic configuration of class loading

Conditional configuration is based on Spring's @Conditional, SpringBoot provides rich conditional configuration:
refer to
@ConditionalOnClass: it takes effect when the class exists in the
classpath @ConditionalOnMissingClass: takes effect when the class does not exist in the classpath
@ConditionalOnBean: the type of bean exists in the DI container Effective when
@ConditionalOnMissingBean: Effective when the type of bean does not exist in the DI container @ConditionalOnSingleCandidate: Effective when
there is only one bean of this type in the DI container or only one
@Primary @ConditionalOnExpression: When the result of the SpEL expression is true
@ConditionalOnProperty: It takes effect when the parameter settings or values ​​are the same
@ConditionalOnResource : It takes effect when the specified file exists
@ConditionalOnJndi : It takes effect when the specified JNDI exists
@ConditionalOnJava : It takes effect when the specified Java version exists
@ConditionalOnWebApplication : It takes effect in the web application environment
@ConditionalOnNotWebApplication : Effective in non-Web application environment

Reference:
http://rensanning.iteye.com/blog/2363467

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325655914&siteId=291194637