preface

There are many articles on the Internet about how Spring Boot integrates XXX. Is there an article that tells you why? Integration of tens of thousands of frames, in fact, routines so a few, why to learn tens of thousands, not as easy to learn a few routines to integrate, it is not sweet??

Today, the purpose of writing this article is to teach you a few routines from the thought, without mentioning what integration to Baidu, to try to personally integrate a.

The Spring version of the Boot

The version of Spring Boot on which this article is based is 2.3.4.release.

1. Find the automatic configuration class

Spring Boot will first add a dependent starter when integrating any component. For example, Mybatis has a Mybatis -spring-boot-starter. The dependency is as follows:

<dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
</dependency>
Copy the code

Each starter basically has an auto-configuration class named in a similar format: XxxAutoConfiguration, such as MybatisAutoConfiguration of Mybatis class is MybatisAutoConfiguration, Redis automatic configuration class is RedisAutoConfiguration, The automatic configuration class for the WEB module is WebMvcAutoConfiguration.

2. Note the annotation @conditionalxxx

Conditionalxxx (@conditionalxxx) Conditionalxxx (@conditionalxxx) Conditionalxxx (@conditionalxxx) Conditionalxxx (@conditionalxxx) Conditionalxxx (@conditionalxxx) Conditionalxxx (@conditionalxxx)

The first thing to note is the @conditionalxxx annotation on the auto-configuration class, which is the condition for it to work.

For example, the WebMvcAutoConfiguration class has an annotation like this:

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
Copy the code

Above is the meaning of this line of code in the IOC container no WebMvcConfigurationSupport when an instance of this class will only take effect automatically configure class, This is why annotating @enableWebMVC on the configuration class invalidates the auto-configuration class WebMvcAutoConfiguration.

Conditionalxxx. Spring Boot will use the @bean and @conditionalxxx annotations in the auto-configuration class to provide some default configuration for the components to run. However, the conditional nature of the @conditionalxxx (effective under certain conditions) annotation makes it easier for developers to overlay these configurations.

For example, MybatisAutoConfiguration class Mybatis has the following method:

  @Bean
  @ConditionalOnMissingBean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {}
Copy the code

The above method does not look at the content of the method body, only the annotations on the method. ConditionalOnMissingBean the @bean annotation means to inject a Bean into the IOC container, and ConditionalOnMissingBean is a conditional judgment, indicating that an object of type SqlSessionFactory does not exist in the IOC container.

Oh? If you want to customize SqlSessionFactory, you can create an object of type SqlSessionFactory and inject it into the IOC container to override the auto-configuration class. For example, when configuring multiple data sources in Mybatis, you need to customize an SqlSessionFactory instead of using the automatic configuration class.

In general, be sure to automatically configure the @conditionalxxx annotation on your class or method to indicate a particular condition.

Common annotations are listed below:

  1. @ConditionalOnBean: instantiates when there are specified beans in the container.
  2. @ConditionalOnMissingBean: instantiates when no Bean is specified in the container.
  3. @ConditionalOnClass: instantiates when the specified class exists on the classpath.
  4. @ConditionalOnMissingClass: instantiates when no class is specified on the classpath.
  5. @ConditionalOnWebApplication: instantiate when the project is a Web project.
  6. @ConditionalOnNotWebApplication: instantiate when the project is not a Web project.
  7. @ConditionalOnProperty: instantiates when the specified property has the specified value.
  8. @ConditionalOnExpression: Conditional judgment based on SpEL expression.
  9. @ConditionalOnJava: Triggers instantiation when the JVM version is in the specified version range.
  10. @ConditionalOnResource: Triggers instantiation when a specified resource is in the classpath.
  11. @ConditionalOnJndi: Triggers instantiation if JNDI exists.
  12. @ConditionalOnSingleCandidate: Triggers instantiation when there is only one specified Bean in the container, or more than one but the preferred Bean is specified.

3. Pay attention to EnableConfigurationProperties annotation

EnableConfigurationProperties the annotation is often marked on the configuration class, makes the @ ConfigurationProperties annotation configuration file to take effect, so that you can in the global configuration file (application. XXX) configuration of the specified prefix attribute.

Mark the following line above the Redis auto-configuration class RedisAutoConfiguration:

@EnableConfigurationProperties(RedisProperties.class)
Copy the code

RedisProperties RedisProperties RedisProperties RedisProperties

@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {
	private int database = 0;
	private String url;
	private String host = "localhost";
	privateString password; .Copy the code

The @configurationProperties annotation specifies that configurations prefixed with Spring.redis. XXX in the global configuration file are mapped to the specified properties of RedisProperties. The RedisProperties class defines the required properties of Redis, such as host, IP address, password, and so on.

@EnableConfigurationPropertiesAnnotations are used to validate a specified configuration and map properties configured in the global configuration file to properties of related classes.

Why pay attention@EnableConfigurationPropertiesWhat about this note?

Introduced after a component is often need to change some configuration, we all know that in the global configuration file can be modified, but don’t know what the prefix, which properties can change, so find @ EnableConfigurationProperties after this annotation can find the corresponding configuration prefix and can modify properties.

4. Note the @import annotation

This annotation is a bit of a nifty one, already available in Spring 3.x, which basically means to quickly import a Bean or configuration class into the IOC container. This note has a lot of great uses and will be covered in a separate post.

@import This annotation is usually annotated above the autoconfiguration class and usually imports one or more configuration classes.

For example, RabbitMQ’s automatic configuration class RabbitAutoConfiguration has the following line:

@Import(RabbitAnnotationDrivenConfiguration.class)
Copy the code

The role of the line of code is added RabbitAnnotationDrivenConfiguration the configuration class, makes Spring the Boot when loaded into the automatic configuration class can load together.

For example, the Redis auto-configuration class RedisAutoConfiguration has the following line of code:

@Import({ LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class })
Copy the code

The @ Import at the same time introduced the Lettuce and Jedis two configuration class, so if you need to use the Redis Jedis as connection pooling, want to know what you want to configure Jedis, should see JedisConnectionConfiguration the configuration class at this time.

Summary: The @import annotation is above the autoconfiguration class, which is usually used to quickly Import one or more configuration classes, so if the autoconfiguration class does not configure something, be sure to look at the @import annotation to Import the configuration class.

5. Note the @autoConfigurexxx annotation

Annotations like @AutoConfigurexxx determine the loading order of auto-configuration classes, Examples are AutoConfigureAfter (after specifying the auto-configuration class), AutoConfigureBefore (before specifying the auto-configuration class), and AutoConfigureOrder (specifying the priority of the auto-configuration class).

Why do you care about order? Mybatis and DataSource are mutually dependent components, such as Mybatis and DataSource. Annotations such as @AutoConfigurexxx solve the problem of interdependencies between components.

For example, MybatisAutoConfiguration is marked with the following line of code:

@AutoConfigureAfter(DataSourceAutoConfiguration.class)
Copy the code

This line of code is very simple, is this automatic configuration in DataSourceAutoConfiguration MybatisAutoConfiguration after the loading, because you need me, how simple reason.

Why Mybatis configuration is good, start will report an error? Let’s see if the data source is configured successfully.

6. Note the internal static configuration classes

Some of the auto-configuration classes are simpler and less predictable, such as RedisAutoConfiguration, which defines two methods for injecting beans.

But some auto-configuration classes are not so simple, and can nest n static configuration classes in the middle, such as WebMvcAutoConfiguration, Class is nested WebMvcAutoConfigurationAdapter, EnableWebMvcConfiguration, ResourceChainCustomizerConfiguration these three configuration classes. If you just look at WebMvcAutoConfiguration, the auto-configuration class doesn’t seem to have much configuration, but there’s a lot going on inside.

Conclusion: Be sure to automatically configure a nested configuration class inside the class.

conclusion

The above summarizes six integration routines, hoping to help readers get rid of Baidu, their own independent integration of components.

In a word, there are many articles about Spring Boot integration of XXX components, I believe we also look at the meng, in fact, the routine is the same, learn to share the routine Chen, let you less detour!!