The following example code is in Github, the code link

**Spring Boot starter ** greatly simplifies the configuration of dependencies in project builds by leveraging transition-dependent resolution to aggregate common libraries into several feature-specific dependencies that are needed to get projects up and running quickly. Such as spring-boot-starter-web starter dependency, Spring-boot-starter, spring-boot-starter- JSON, spring-boot-starter-tomcat, spring-web, spring-webMVC, etc. This eliminates the need to introduce multiple Web-related dependencies and resolve version conflicts in our Web projects.

The official starter naming format is spring-boot-starter-*, Such as spring-boot-starter-actuator, Spring-boot-starter – Web, spring-boot-starter-test and other official starters, The common naming format for starter is thirdproject-spring-boot-starter

  • The demo-spring-boot-starter project structure is to write a custom demo-spring-boot-starter and introduce two JAR packages demo1 and demo2, and then introduce demo-spring-boot-starter in the project project, as shown in the following figure:

I. Demo1 Project

(1) POM files are as follows:

<? The 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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Fiuty < / groupId > < artifactId > not < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < project >Copy the code

(2) Demo1 has only one Student class:

@Data
public class Student {

    private String name;

    private Integer age;
}
Copy the code
Second, the demo2

(1) POM files are as follows:

<? The 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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Fiuty < / groupId > < artifactId > demo2 < / artifactId > < version > 1.0 - the SNAPSHOT < / version > < project >Copy the code

(2) Demo2 also has only one Teacher class:

@Data
public class Teacher {

    private String name;

    private Integer age;

}
Copy the code
3. Demo-spring-boot-starter project

(1) The starter definition requires the introduction of spring-boot-starter dependency, spring-boot-Autoconfigure dependency, and third-party JAR demo1 and DEMO2 dependencies. If you need toconfigure meta-information, You also need to introduce the spring-boot-configuration-processor dependency. Demo1 and DEMO2 poM files:

        <dependency>
            <groupId>com.fiuty</groupId>
            <artifactId>demo1</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.fiuty</groupId>
            <artifactId>demo2</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
Copy the code

The complete POM file is as follows:

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.4.0 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.fiuty</groupId> < artifactId > demo - spring - the boot - starter < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > <name>demo-spring-boot-starter</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.fiuty</groupId> <artifactId>demo1</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.fiuty</groupId> <artifactId>demo2</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

(3) Demo-spring-boot-starter has a DemoConfiguration class (injected with two beans, student of Demo1 and teacher of Demo2). There are also two properties configuration classes that read configuration values from yamL files, as well as the DemoAutoConfiguration class and the spring.Factories auto-assembly configuration file that initializes beans at project startup and is managed by the Spring container:

  • TeacherProperties Configuration class:
@ConfigurationProperties(prefix = "com.demo2") @Configuration @Data public class TeacherProperties { private String name  = "defaultTeacher"; private Integer age = 30; }Copy the code
  • StudentProperties configuration class
@ConfigurationProperties(prefix = "com.demo1") @Configuration @Data public class StudentProperties { private String name  = "defaultStudent"; private Integer age = 10; }Copy the code
  • The DemoConfiguration class assigns the yamL file configuration value to two beans, student of Demo1 and teacher of Demo2:
@ Configuration / / allowed to import the Configuration, the Configuration class bean was managed by the spring container @ EnableConfigurationProperties ({StudentProperties. Class, TeacherProperties.class}) @Slf4j public class DemoConfiguration { @Bean public Student getStudent(StudentProperties studentProperties) { Student student = new Student(); Log.info (" initialize student"); student.setName(studentProperties.getName()); student.setAge(studentProperties.getAge()); return student; } @Bean public Teacher getTeacher(TeacherProperties teacherProperties) { Teacher teacher = new Teacher(); teacher.setName(teacherProperties.getName()); teacher.setAge(teacherProperties.getAge()); return teacher; }}Copy the code
  • DemoAutoConfiguration Automatic assembly class
@import (democonfiguration.class) @configuration public class DemoAutoConfiguration {}Copy the code
  • META-INF Spring. factories (resource

The Spring container registers the associated autoloader beans when they are introduced into the starter.

# Auto Confiure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.fiuty.demo.spring.boot.starter.configure.DemoAutoConfiguration,\
com.fiuty.demo.spring.boot.auto.assemble.AssembleAutoConfiguration
Copy the code
4

(1) The project structure is as follows: introduce the custom starter, configure the two configuration classes of starter in the YAML file, and finally inject the student bean of Demo1 and teacher bean of Demo2 to obtain their name and age values:

(2) The poM file of the starter is as follows:

<dependency> <groupId>com.fiuty</groupId> <artifactId>demo-spring-boot-starter</artifactId> < version > 0.0.1 - the SNAPSHOT < / version > < / dependency >Copy the code

Complete POM file:

<? The 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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion > 4.0.0 < / modelVersion > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.4.0 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.fiuty</groupId> <artifactId>project</artifactId> <version>0.0.1-SNAPSHOT</version> <name>project</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> < the groupId > com. Fiuty < / groupId > < artifactId > demo - spring - the boot - starter < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>Copy the code

(3) The code is as follows

  • DemoController
@RestController @RequestMapping("/api") public class DemoController { @Autowired private DemoService demoService; @GetMapping("/student/info") public String studentInfo() { return demoService.studentInfo(); } @GetMapping("/teacher/info") public String info() { return demoService.teacherInfo(); }}Copy the code
  • DemoService
public interface DemoService {

    String studentInfo();

    String teacherInfo();

}
Copy the code
  • DemoServiceImpl
@Service public class DemoServiceImpl implements DemoService { @Autowired private Student student; @Autowired private Teacher teacher; @Override public String studentInfo() { return student.getName() + ":" + student.getAge(); } @Override public String teacherInfo() { return teacher.getName() + ":" + teacher.getAge(); }}Copy the code
  • application.yaml
com:
  demo1:
    age: 18
    name: zhangsan
  demo2:
    name: lili
    age: 25
Copy the code

When writing the YAML file, you can see the related prompts, as shown in the following figure:

  • Finally the project runs and the browser visits: