手寫spring boot starter起步依賴demo

以下例子代碼在github中,代碼鏈接

Spring Boot起步依賴(starter)大大簡化了項目構建中的依賴配置,利用了傳遞依賴解析,把常用庫聚合在一起,組成了幾個爲特定功能而定製的依賴,這些依賴項是使項目快速啓動和運行所需的依賴項,如spring-boot-starter-web起步依賴,聚合了spring-boot-starter、spring-boot-starter-json、spring-boot-starter-tomcat、spring-web、spring-webmvc等依賴,使得我們的web項目無需引入多個web相關的依賴項以及爲了解決版本衝突等問題。

官方starter命名的格式是spring-boot-starter-*,如spring-boot-starter-actuator、spring-boot-starter-web、spring-boot-starter-test等官方starter,平常我們自己定義的starter一般命名格式是thirdProject-spring-boot-starter

  • 本文示例代碼項目結構是寫一個自定義的demo-spring-boot-starter引入兩個jar包demo1和demo2,然後在project項目中引入demo-spring-boot-starter,項目結構如圖所示:


一、demo1項目

(1)pom文件如下:

<?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>com.fiuty</groupId>
    <artifactId>demo1</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

(2)demo1只有一個Student類:

@Data
public class Student {

    private String name;

    private Integer age;
}
二、demo2

(1)pom文件如下:

<?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>com.fiuty</groupId>
    <artifactId>demo2</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>

(2)同樣,demo2也只有一個類Teacher:

@Data
public class Teacher {

    private String name;

    private Integer age;

}
三、demo-spring-boot-starter項目

(1)定義starter需要引入spring-boot-starter依賴、spring-boot-autoconfigure依賴以及第三方jar demo1和demo2的依賴,如果需要配置元信息的話,還需引入spring-boot-configuration-processor依賴。
(2)引入demo1和demo2的pom文件:

        <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>

完整的pom文件如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.fiuty</groupId>
    <artifactId>demo-spring-boot-starter</artifactId>
    <version>0.0.1-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>

(3)demo-spring-boot-starter的代碼結構如下圖所示,有一個AutoConfigure配置類(注入了兩個bean,demo1的student和demo2的teacher),還有兩個properties配置類用於讀取yaml文件的配置值,還有一個配置元信息spring.factories關聯兩個properties配置類使得寫yaml文件時發現配置文件:


  • TeacherProperties配置類:
@Configuration
@ConfigurationProperties(prefix = "com.demo2")
@Data
public class TeacherProperties {

    private String name;

    private Integer age;
}
  • StudentProperties配置類
@ConfigurationProperties(prefix = "com.demo1")
@Configuration
@Data
public class StudentProperties {

    private String name;

    private Integer age;
}
  • AutoConfigure配置類,將yaml文件的配置值賦值到兩個bean,demo1的student和demo2的teacher:
@Configuration
public class AutoConfigure {

    @Autowired
    private StudentProperties studentProperties;

    @Autowired
    private TeacherProperties teacherProperties;

    @Bean
    public Student getStudent() {
        Student student = new Student();
        student.setName(studentProperties.getName());
        student.setAge(studentProperties.getAge());
        return student;
    }

    @Bean
    public Teacher getTeacher() {
        Teacher teacher = new Teacher();
        teacher.setName(teacherProperties.getName());
        teacher.setAge(teacherProperties.getAge());
        return teacher;
    }
}
  • META-INF spring.factories配置發現配置文件
# Auto Confiure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.fiuty.demo.spring.boot.starter.configure.AutoConfigure
四、project項目

(1)project項目結構如下,引入自定義starter,在yaml文件配置starter的兩個配置類,最後注入demo1的student bean和demo2的teacher bean,獲取其name和age的值:



(2)引入自定義的starter的pom文件如下:

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

完整的pom文件:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-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>
            <groupId>com.fiuty</groupId>
            <artifactId>demo-spring-boot-starter</artifactId>
            <version>0.0.1-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>

(3)代碼如下

  • 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();
    }
}
  • DemoService
public interface DemoService {

    String studentInfo();

    String teacherInfo();

}
  • 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();
    }
}
  • application.yaml
com:
  demo1:
    age: 18
    name: zhangsan
  demo2:
    name: lili
    age: 25

因爲寫了發現配置文件spring.factories,故在寫yaml文件的時候可以有提示,如下圖所示:

  • 最後項目跑起來,瀏覽器訪問:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章