In fact, you can also use SpringBoot to customize the starter

Anyone who has used SpringBoot should know that a SpringBoot project is composed of a Starter, and a Starter represents the SpringBoot startup dependency of the project. In addition to the official Starter, we can customize the new Starter according to our needs.

One, customize SpringBoot Starter

To customize the Starter, the first need to realize the automatic configuration, and to realize the automatic configuration, the following two conditions must be met:

(1) It can automatically configure the configuration information required by the project, that is, automatically load the dependent environment;

(2) Bean can be automatically generated according to the information provided by the project and registered in the Bean management container;

To achieve automatic configuration, you need to introduce the following dependencies in the project's pom.xml file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>

The implementation process of customizing Starter according to needs is roughly as follows (take the Starter defined by me as an example):

Project directory structure:

Insert picture description here
1. Introduce the configuration dependency of the project

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>

2. Create the xxxService class and complete the related operation logic

Code: StringService.java

public class StringService {
    
    

    private String str1;

    private String str2;

    private String default_str;

    public String getStr1() {
    
    
        return str1;
    }

    public void setStr1(String str1) {
    
    
        this.str1 = str1;
    }

    public String getStr2() {
    
    
        return str2;
    }

    public void setStr2(String str2) {
    
    
        this.str2 = str2;
    }

    public String getDefault_str() {
    
    
        return default_str;
    }

    public void setDefault_str(String default_str) {
    
    
        this.default_str = default_str;
    }

    public String addStr(){
    
    
        if(str1 != null){
    
    
            if(str2 != null){
    
    
                return str1 + "," + str2;
         }
            return str1;
        }
        return default_str;
    }

}

3. Define the xxxProperties class, the property configuration class, and complete the operations related to property configuration, such as setting the property prefix for configuration in application.properties

Code: StringProperties.java

//指定项目在属性文件中配置的前缀为str,即可以在属性文件中通过 str.str1=springboot,就可以改变属性类字段 str1 的值了
@SuppressWarnings("ConfigurationProperties")
@ConfigurationProperties(prefix = "str")
public class StringProperties {
    
    

    public static final String DEFAULT_STR1 = "I know, you need me";

    public static final String DEFAULT_STR2 = "but I also need you";

    private String str1 = DEFAULT_STR1;

    private String str2 = DEFAULT_STR2;

    public String getStr1() {
    
    
        return str1;
    }

    public void setStr1(String str1) {
    
    
        this.str1 = str1;
    }

    public String getStr2() {
    
    
        return str2;
    }

    public void setStr2(String str2) {
    
    
        this.str2 = str2;
    }
}

4. Define xxxConfigurationProperties class, automatic configuration class, used to complete Bean creation and other tasks

Code: StringAutoConfiguration.java

// 定义 java 配置类
@Configuration
//引入StringService
@ConditionalOnClass({
    
    StringService.class})
// 将 application.properties 的相关的属性字段与该类一一对应,并生成 Bean
@EnableConfigurationProperties(StringProperties.class)
public class StringAutoConfiguration {
    
    

    // 注入属性类
    @Autowired
    private StringProperties stringProperties;

    @Bean
    // 当容器没有这个 Bean 的时候才创建这个 Bean
    @ConditionalOnMissingBean(StringService.class)
    public StringService helloworldService() {
    
    
        StringService stringService = new StringService();
        stringService.setStr1(stringProperties.getStr1());
        stringService.setStr2(stringProperties.getStr2());
        return stringService;
    }

}

5. Create a directory META-INF under resources, and create spring.factories under the META-INF directory. When SpringBoot starts, the automation configuration class of the project will be loaded according to this file

Code: spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.lhf.springboot.config.StringAutoConfiguration

6. The custom Starter is defined here, and it can be used only by introducing it in other projects.

2. Use custom Starter in other projects

1. Introduce custom Starter dependency configuration in the new project

Create a new SpringBoot project and introduce the dependency configuration of the custom SpringBoot Starter in the project's pom.xml file as follows:

<!--引入自定义Starter-->
<dependency>
    <groupId>com.lhf.springboot</groupId>
    <artifactId>spring-boot-starter-string</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2. Write a simple Controller

@RestController
public class StringController {
    
    

      @Autowired
    private StringService stringService;  //引入自定义Starter中的StringService

    @RequestMapping("/")
      public String addString(){
    
    
        return stringService.addStr();
    }
}

3. Write the attribute configuration file, the content is as follows:

#配置自定义的属性信息
str.str1=为什么我的眼里常含泪水
str.str2=那是因为我对你爱的深沉

4. Start the project to visit, the effect is shown in the figure:

Insert picture description here
Insert picture description here

Conclusion:

At this point, the process and usage of SpringBoot custom Starter is over, and that's it for sharing! If you still need Java learning materials or interview materials, you can click to enter, code: csgg , free of charge!
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_45270667/article/details/109258778