美文网首页
手写Spring Boot Starter起步依赖demo

手写Spring Boot Starter起步依赖demo

作者: dd_123 | 来源:发表于2020-12-06 14:58 被阅读0次

以下例子代码在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,项目结构如图所示:


    项目结构.png
一、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的代码结构如下图所示,有一个DemoConfiguration配置类(注入了两个bean,demo1的student和demo2的teacher),还有两个properties配置类用于读取yaml文件的配置值,还有自动装配类DemoAutoConfiguration和自动装配配置文件spring.factories,用于项目启动时初始化bean并且被spring容器管理:

demo-spring-boot-starter的代码结构.png
  • TeacherProperties配置类:
@ConfigurationProperties(prefix = "com.demo2")
@Configuration
@Data
public class TeacherProperties {

    private String name = "defaultTeacher";

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

    private String name = "defaultStudent";

    private Integer age = 10;
}
  • DemoConfiguration配置类,将yaml文件的配置值赋值到两个bean,demo1的student和demo2的teacher:
@Configuration
//允许导入配置类,配置类bean被spring容器管理
@EnableConfigurationProperties({StudentProperties.class, TeacherProperties.class})
@Slf4j
public class DemoConfiguration {

    @Bean
    public Student getStudent(StudentProperties studentProperties) {
        Student student = new Student();
        log.info("初始化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;
    }
}
  • DemoAutoConfiguration自动装配类
//自动装配,项目启动即在spring容器中注册管理相关bean
@Import(DemoConfiguration.class)
@Configuration
public class DemoAutoConfiguration {
}
  • META-INF spring.factories配置自动装配类,需要注意的是spring.factories需放在resource
    的目录下,在引入starter的时候spring容器会注册相关自动装配bean。
# Auto Confiure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.fiuty.demo.spring.boot.starter.configure.DemoAutoConfiguration,\
com.fiuty.demo.spring.boot.auto.assemble.AssembleAutoConfiguration
四、project项目

(1)project项目结构如下,引入自定义starter,在yaml文件配置starter的两个配置类,最后注入demo1的student bean和demo2的teacher bean,获取其name和age的值:


project项目结构.png

(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

在写yaml文件的时候可以看到有相关提示,如下图所示:

发现配置前缀.gif 发现配置1.png 发现配置2.png
  • 最后项目跑起来,浏览器访问:


    浏览器1.png
浏览器2.png

相关文章

网友评论

      本文标题:手写Spring Boot Starter起步依赖demo

      本文链接:https://www.haomeiwen.com/subject/wtwfbktx.html