Spring Boot - In-depth Principles - Customize starter

Posted by moe on Mon, 15 Jun 2020 21:25:55 +0200

2.2.3 Custom starter

Spring Boot is made up of many Starters (a series of starter plug-ins that are automatically configured), and it is also because of starter that Spring Boot is popular.

starter is a very important part of Spring Boot and can be understood as a pluggable plug-in because it allows developers who use a feature to ignore the processing of dependent libraries and do not need specific configuration information. Spring Boot automatically discovers the required beans through classpath classes and weaves them into the corresponding beans.

For example, if we need a Redis plug-in, we can use spring-boot-starter-data-redis, and if we need MongoDB, we can use spring-boot-starter-data-mongodb

2.2.3.1 Why do I need to customize starter

During development, there are often configuration modules that are independent of the business.If we encapsulate these functional configurations outside of stand-alone business code as starter s, we only need to reuse them inPom.xmlReference to dependencies is sufficient, and Spring Boot helps us complete automatic assembly.

2.2.3.2 Custom starter naming rules

The starter provided by Spring Boot is named after spring-boot-starter-xxxx and is officially recommended for custom starters

The xxxx-spring-boot-starter naming rules distinguish the starters provided by the Spring Boot ecology.

2.2.3.3 Customization
2.2.3.3.1 custom-spring-boot-starter
  1. New Maven project, named custom-spring-boot-starter, import dependencies

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
    
       <groupId>org.example</groupId>
       <artifactId>custom-spring-boot-starter</artifactId>
       <version>1.0-SNAPSHOT</version>
       <packaging>jar</packaging>
    
       <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
           <java.version>1.8</java.version>
       </properties>
    
       <dependencies>
           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-autoconfigure</artifactId>
               <version>2.1.14.RELEASE</version>
           </dependency>
       </dependencies>
    </project>
  2. Writing JavaBean s

    @EnableConfigurationProperties(CustomBean.class)
    @ConfigurationProperties(prefix = "custom")
    public class CustomBean {
    
       private Integer id;
    
       private String name;
    
       public Integer getId() {
           return id;
       }
    
       public void setId(Integer id) {
           this.id = id;
       }
    
       public String getName() {
           return name;
       }
    
       public void setName(String name) {
           this.name = name;
       }
    }
  3. Write Configuration Class

    @Configuration
    // Automatic configuration when a specified class exists under the classpath of the class path
    @ConditionalOnClass
    public class CustomConfig {
    
       @Bean
       public CustomBean customBean() {
           return new CustomBean();
       }
    
    }
  4. Create META-INF/under resources Spring.factories

    Note: META-INF directory andSpring.factoriesFile needs to be created manually

   org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
   site.luojie.custom.config.CustomConfig
2.2.3.3.2 Using custom-spring-boot-starter
  1. Import custom starter dependencies

    <dependency>
       <groupId>org.example</groupId>
       <artifactId>custom-spring-boot-starter</artifactId>
       <version>1.0-SNAPSHOT</version>
    </dependency>
  2. Write a configuration file

    custom.id=1
    custom.name=test
  3. unit testing

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class LearnApplicationTest {
    
       @Autowired
       private CustomBean customBean;
    
       @Test
       public void testCustomBean(){
           System.out.println(customBean);
       }
    }

Topics: Web Development Spring Maven Apache Redis