preface

Some of the initiators provided by Spring Boot may be sufficient for day-to-day work, but it is inevitable that you will need to customize the initiators, such as integrating an unfamiliar component, to work out of the box.

In the last chapter section from the bottom of the source code introduced the principle of Spring Boot automatic configuration, have not read the friend recommended to see: Spring Boot automatic configuration source code analysis

This article will show you how to customize an initiator and give a detailed analysis of the order in which the auto-configuration classes are executed.

How to customize a starter?

Starter core is automatic configuration, the chapter has introduced automatic configuration source code analysis, AutoConfigurationImportSelector from spring. Factories in loading configuration class automatically, so you just need to set the automatic configuration of the custom class in the file.

Read the source code of friends know some common automatic configuration class annotations, summarized as follows:

  1. @ConfigurationThis annotation indicates that this is a configuration class,Auto-configuration classes can be left out of this annotation.
  2. @EnableConfigurationProperties: This configuration is also used frequently to make the specified property configuration take effect. Generally, auto-configuration classes need to read the configuration taken from the definition from the global property configuration, which is a switch.
  3. @ConditionalOnXxxx: This annotation is the core of the auto-configuration class. The auto-configuration class should be automatically configured on startup and ensure that the user’s user-defined configuration overrides the auto-configuration. This annotation is a conditional statement, and an operation will be performed only when the specified condition is true. If you don’t understand, please look at the previous article of the author:Spring Boot = Spring Boot = Spring Boot
  4. @AutoConfigureAfter: Specifies the order in which the automatic configuration classes are executed.
  5. @AutoConfigureBefore: Specifies the order in which automatic configuration columns are executed, as described below.
  6. @AutoConfigureOrder: Specifies the priority of the automatic configuration class, as described later.

With that in mind, customizing a starter is a simple two-step process.

1. Prepare your own auto-configuration classes

The soul of the initiator is the automatic configuration class, so you need to create an automatic configuration class, as follows:

@Configuration
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE+5)
@ConditionalOnProperty(prefix = "my.auto",name = "enabled",havingValue = "true",matchIfMissing = true)
public class MyCustomAutoConfiguration {}Copy the code

The above automatic configuration class is simply written by the author in accordance with the format. In real development, the default configuration needs to be made according to the services of the initiator.

2. Set the auto-configuration classes in Spring.Factories

An auto-configuration class annotated with @Configuration is just a normal Configuration class if you don’t put it in a Spring.Factories file. To make it an auto-configured class, you need to set it up in the spring.Factories file as follows:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.autoconfig.MyCustomAutoConfiguration
Copy the code

After the above configuration, the rough launcher is complete, just need to package, and Maven can be introduced to work.

How do I specify the order in which auto-configuration classes are executed?

Do auto-configuration classes need to define an order of execution? Answer: Yes. SqlSessionFactory = SqlSessionFactory = SqlSessionFactory = SqlSessionFactory

How do YOU customize the order in which the auto-configuration classes are executed? Here are the three notes I mentioned above:

  1. @AutoConfigureAfter: The current configuration class is executed after the specified configuration class
  2. @AutoConfigureBefore: The current configuration class is executed before the specified configuration class
  3. @AutoConfigureOrder: Specifies the priority. A smaller value indicates a higher priority.

Share a classic myth

Code written by people who don’t know much about Spring Boot is really ugly. I’ve seen people using @autoConfigurexxx annotations on common configuration classes as follows.

@Configuration
@AutoConfigureBefore(Config2.class)
public class Config1{}

@Configuration
public class Config2{}
Copy the code

Does not feel very cool, the original can also specify the configuration of the execution order….. (ten thousand words omitted)

Maybe sometimes the bad luck gives you an illusion that the configuration is really successful. In fact, this approach is not feasible, and the above three annotations only apply to auto-configured classes.

How are source code analysis auto-configuration classes sorted?

Key code is actually in AutoConfigurationImportSelector will automatically configure class from the spring. The factories after loading out will be sorted according to the condition, in selectImports () method of the last line of code is as follows:

return sortAutoConfigurations(processedConfigurations, getAutoConfigurationMetadata()).stream()
					.map((importClassName) -> new Entry(this.entries.get(importClassName), importClassName))
					.collect(Collectors.toList());
Copy the code

The code above is to sort good automatic configuration class return, follow up the code, and found that the final implementation in AutoConfigurationSorter. GetInPriorityOrder () method, the logic is as follows:

The specific process is shown in the figure above, and the sequence is as follows:

  1. Let’s start with alphabetical order
  2. In accordance with the@AutoConfigureOrderPriority sorting
  3. Finally, in accordance with the@AutoConfigureAfter,@AutoConfigureBeforeThe sorting

As you can see from the configuration order above, the final decision is made by @AutoConfigureAfter and @AutoConfigureBefore annotations.

conclusion

This article describes how to customize an initiator and specify the automatic configuration class execution order, through the author’s introduction, hope readers can understand and flexible use.