Spring Boot auto configuration

In @SpringBootApplication , it is mentioned that @EnableAutoConfiguration can use the SpringFactoriesLoader feature to summarize and load the JavaConfig class of "Configuration" with @Configuration into the final ApplicationContext, but this is actually only a "simplified version" of the description.

In fact, the automatic configuration function based on @EnableAutoConfiguration has more powerful regulation and control capabilities. By matching with, for example, condition-based configuration capabilities or adjusting the loading order, we can perform more fine-grained adjustment and control of automatic configuration.

Condition-based automatic configuration

Condition-based automatic configuration comes from the "condition-based configuration" feature in the Spring framework. In the Spring framework, we can use @Conditional Annotation with @Configuration or @Bean and other Annotation to intervene whether a configuration or bean definition can take effect. Its final implementation effect or semantic is similar to the following pseudo code:

if (meets the conditions specified by @Conditional) {
    load current configuration (enable current Configuration) or register current bean definition;
}

To implement condition-based configuration, we only need to specify our own Condition implementation class via @Conditional (can be applied to the annotation of type Type or the annotation of method Method):

@Conditional({MyCondition1.class, MyCondition2.class, ...})

The most important thing is that @Conditional can be used as a Meta Annotation to annotate other Annotation implementation classes to build a variety of composite Annotation. For example, SpringBoot's autoconfigure module implements a batch of Annotation (located at org. springframework.boot.autoconfigure.condition package), the conditions are as follows:

  • @ConditionalOnBean: When there is a specified Bean in the container.
  • @ConditionalOnClass: When there is a specified class under the class path.
  • @ConditionalOnExpression: Based on SpEL expression as a judgment condition.
  • @ConditionalOnJava: Based on the JVM version as the judgment condition.
  • @ConditionalOnJndi: Find the specified location under the condition that JNDI exists.
  • @ConditionalOnMissingBean: When there is no Bean specified in the container.
  • @ConditionalOnMissingClass: When there is no specified class under the class path.
  • @ConditionalOnNotWebApplication: Under the condition that the current project is not a Web project.
  • @ConditionalOnProperty: Whether the specified property has the specified value.
  • @ConditionalOnResource: Whether the class path has a specified value.
  • @ConditionalOnSingleCandidate: When there is only one specified bean in the container, or there are multiple but specified preferred beans.
  • @ConditionalOnWebApplication: Under the condition that the current project is a Web project.


With the cooperation of these composite annotations, we can combine @ EnableAuto-Configurationn to implement condition-based automatic configuration.

SpringBoot can be popular, a large part of the credit is due to a series of automatically configured dependency modules provided in advance, and these dependency modules are implemented based on the above @Conditional composite Annotation, which also means that all these dependency modules are based on What needs to be loaded, these dependent modules will only take effect if they meet certain specific conditions, which is what we call "smart" automatic configuration.

Adjust the order of automatic configuration

In the process of implementing automatic configuration, in addition to providing condition-based configuration, we can also adjust the current configuration or component loading order accordingly, so that the dependency analysis and assembly between these configurations or components can be successfully completed .

We can use @ org.springframework.boot.autoconfigure.AutoConfigureBefore or @ org.springframework.boot.autoconfigure.AutoConfigureAfter to make the current configuration or component before or after some other component, for example, suppose we want certain JMX operations related The bean definition is done after the MBeanServer configuration is complete, then we can provide a configuration similar to the following:

@Configuration
@AutoConfigureAfter(JmxAutoConfiguration.class)
public class AfterMBeanServerReadyConfiguration {
    @AutoWired
    MBeanServer mBeanServer;
    //通过 @Bean 添加必要的 bean 定义
}

So far, we have completed a basic analysis of the core components of SpringBoot. In general, most of the things are some of the original concepts and practices behind the Spring framework. SpringBoot is only implemented in these concepts and practices for specific scenarios. The curing and sublimation have been carried out, and it is precisely these curings that make it more convenient and efficient for us to develop applications based on the Spring framework. 

 

Published 203 original articles · won praise 6 · views 4488

Guess you like

Origin blog.csdn.net/weixin_42073629/article/details/105462645