5 steps to take you through the knowledge points of SpringBoot custom automatic configuration

At present, the SpringBoot framework is really popular among developers. After all, its biggest feature is: quickly build a Spring-based application framework, and it provides various default functions and configurations, allowing developers to quickly build applications. basic structure.

However, when we need to customize some configurations, we need to use custom auto-configuration. Today, I will let you deeply experience the knowledge points of dry goods, and the whole process will be explained directly with code cases, so don't miss it!

1. Automatic configuration class

First of all, we have to learn automatic configuration. We must know what the automatic configuration class is. As the old saying goes, go first and then run.

So everyone must remember that the technical automatic configuration class is a very important part of SpringBoot. That is to say, through automatic configuration, SpringBoot can automatically configure all the components required by the application according to the dependencies of the application. If we want to customize our own automatic configuration, first we need to create a class and mark it as an automatic configuration class with @Configuration and @ConditionalOnClass annotations. For example, we can create a class called MyAutoConfiguration and add the following annotations to it:

@Configuration
@ConditionalOnClass(MyService.class)
public class MyAutoConfiguration {
    
    
    // 自定义配置代码
}

As you can see, in the above code we define a MyAutoConfigurationclass and use @Configurationannotations to mark it as a configuration class. We also use @ConditionalOnClassannotations to specify MyServicethat the auto-configuration class will only be created if the class exists. MyServiceThis also means that our custom auto-configuration will only be applied automatically if the class exists in our application .

So the advantage of custom automatic configuration is that we can customize the configuration of the application according to our own needs. For example, we can define our own data sources, or add our own interceptors. However, when you customize the configuration, you need to customize the attributes to assist. Don't worry, let's continue to take a look!

2. Custom attributes

Through the above we understand what custom configuration is, so everyone should know that when customizing automatic configuration, we often need to use custom properties to configure applications. @ConfigurationPropertiesWe can implement custom properties by using annotations. For example, we can create a MyPropertiesclass called and add the following annotations to it:
Please add a picture description

In the above code, we define a MyPropertiesclass and use @ConfigurationPropertiesannotations to specify that the property prefix of this class is my. application.propertiesThis means, we can use the my.name and my.version properties in the application's files to configure MyPropertiesthe property values ​​of the class.

The advantage of custom properties is that we can configure the properties of the application according to our own needs. For example, we can define our own database connection pool size, or define our own log level. Everyone must practice hard and use what you have learned!

3. Customize Starter

Ok, after learning about automatic configuration and automatic properties, let's take a look at what is a custom Starter, shall we?

In fact, customizing Starter is a very important part of SpringBoot. A Starter is a collection of dependencies that together provide a set of functionality. For example, SpringBoot provides a Starter called spring-boot-starter-web, which includes all the dependencies needed to build web applications. We can package our auto-configuration classes and custom properties by creating a custom Starter. To create a custom Starter, we need to create a Maven project called spring-boot-starter-{name} and add the following dependencies to it:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
        <groupId>{your-group-id}</groupId>
        <artifactId>{your-artifact-id}</artifactId>
        <version>{your-version}</version>
    </dependency>
</dependencies>

Among them, {your-group-id}, {your-artifact-id} and {your-version} are the Maven coordinates of your auto-configuration class respectively.

The nice thing about custom starters is that we can package all custom configuration into a single dependency and add it to the application. In this way, we can package our application into an executable Jar file without worrying about dependencies.

After the introduction, there must be many friends who are curious about how to use the custom Starter, so let's take everyone to learn about the use of the custom Starter.

4. Using a custom Starter in the application

In the Spring framework, Starter is a conventional way to automatically configure the application. When we introduce a Starter dependency, Spring will automatically configure the application according to the automatic configuration class in the Starter. Next, let's take a look at how to use a custom Starter to automatically configure the application.

4.1 Create a custom Starter

To create a custom Starter, we need to create a Maven project and package it as a Spring Boot Starter. In the Starter project, we need to create the following files:

Automatic configuration class: This class contains the automatic configuration logic of the Spring Boot application. We need to define some beans in this class, and their dependencies.

Spring.factories file: All auto-configuration classes are listed in this file. Spring Boot will automatically load this file and use the auto-configuration classes in it with the application.

pom.xml file: This file lists the dependencies of Starter.

Here is an example of a simple auto-configuration class:


@Configuration
public class MyAutoConfiguration {
    
    

    @Bean
    public MyBean myBean() {
    
    
        return new MyBean();
    }

}

4.2 Introducing a custom Starter

To introduce a custom Starter, we need to add a Starter dependency in the application's pom.xml file. For example, if we have a Starter project called "my-spring-boot-starter", we can add the following dependency to the application's pom.xml file:


<dependency>
    <groupId>com.example</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>

When we add this dependency, Spring Boot will automatically load the auto-configuration classes in Starter and auto-configure the application based on these classes.

4.3 Using a custom Starter

Once we have introduced a custom Starter, we can use the beans defined in it. For example, if we define a Bean named "MyBean" in our custom Starter, we can use the following code in the application to get it:

@Autowired
private MyBean myBean;

In this example, Spring will automatically inject "MyBean" and add it to the application context.

The above is how to create, introduce and use customization Spring Boot Starter. Everyone knows that by using a custom Starter, we can reduce the repetitive work of application configuration and improve the maintainability and scalability of the application. So I hope the above content can help you!

Speaking of which, do you know the priority of SpringBoot automatic configuration? If you are interested, just keep reading!

5. SpringBoot automatic configuration priority

In a Spring Boot application, auto-configuration is a powerful feature that automatically configures every aspect of the application, from data sources to web security and more. By default, SpringBoot will automatically configure many parameters, but when there are multiple automatic configuration classes, SpringBoot needs to determine which automatic configuration class should be used first. Next, I will discuss with you the priority of SpringBoot automatic configuration and how to control it.

5.1 Priority of SpringBoot automatic configuration

The priority of SpringBoot's automatic configuration is determined according to the order of jar packages in the classpath. Specifically, when the SpringBoot application starts, SpringBoot checks all available jar packages in the classpath and loads the auto-configuration classes in the following order:

● The jar package of the project itself

● The jar packages that the project depends on

● Third-party jar packages

This means that if the project itself contains auto-configuration classes, it will be loaded in priority over auto-configuration classes in dependent jar packages.

5.2 Controlling the priority of automatic configuration

If you need to control the priority of SpringBoot auto-configuration, you can @AutoConfigureOrderdo so by using annotations. This annotation allows you to specify the order of autoconfiguration classes. The auto-configuration class with the lower number has higher priority.

Here is an example using the @AutoConfigureOrder annotation:

@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
public class MyAutoConfiguration {
    
    
   //自动配置代码
}

In the above example, we have used @AutoConfigureOrderannotation to MyAutoConfigurationset the priority of the class to the highest priority.

5.3 Examples

Let's say we have two auto-configuration classes: MyAutoConfiguration1and MyAutoConfiguration2, which we want to ensure take MyAutoConfiguration1precedence over MyAutoConfiguration2. We can use @AutoConfigureOrderannotations to control their priority. Here is a code example:

5.3.1 MyAutoConfiguration1

@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
public class MyAutoConfiguration1 {
    
    
   //自动配置代码
}

5.3.2 MyAutoConfiguration2

@Configuration
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public class MyAutoConfiguration2 {
    
    
   //自动配置代码
}

In the example above, we have used @AutoConfigureOrderannotations MyAutoConfiguration1to set the priority of the to the highest priority and MyAutoConfiguration2the priority of the to the lowest priority.

6. Summary

In fact, in SpringBoot applications, automatic configuration is a powerful feature that can automatically configure all aspects of the application. By default, SpringBoot will determine the priority of automatic configuration based on the order of jar packages in the classpath. If you need to control the priority of automatic configuration, you can use @AutoConfigureOrderannotations to achieve it.

And by customizing automatic configuration, we can configure the application according to our own needs, making the application more flexible and efficient. So I hope the above content can help you better understand the knowledge of SpringBoot custom automatic configuration, so as to better apply them to your own projects.


Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/131312362