SpringBoot——自定义starter分析

一、自定义starter

  • 启动器只用来做依赖导入

  • 专门来写一个自动配置模块;

  • 启动器依赖自动配置模块,项目中只需要引入相应的starter就会引入启动器的所有传递依赖

在这里插入图片描述

也就是说xxx-starter(启动器)依赖于xxxx-starter-autoconfigurer(自动配置), 别人要使用,就依赖xxx-starter启动器,就自动引入了自动配置。

1、启动器

启动器模块是一个空JAR文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库。

2、命名规约

  • 官方命名

    spring-boot-starter-模块名

    eg:spring-boot-starter-web、spring-boot-starter-jdbc、spring-boot-starter-thymeleaf

  • 自定义命名

    模块名-spring-boot-starter

    eg: mybatis-spring-boot-start

3、如何编写自动配置

@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中
public class XxxxAutoConfiguration {

自动配置类要能加载,需要将启动就加载的自动配置类配置在META-INF/spring.factories

二、案例 (自定义启动器)

  • 首先创建一个项目, 这个项目中创建两个模块; 一个模块是一个Maven模块(启动器), 一个模块是一个SpringBoot模块(自动配置模块)
    在这里插入图片描述

1、自动配置模块

  • 1.创建一个自动配置模块,和创建普通springboot项目一样,不需要引入其他starter
  • 删除掉多余的文件和依赖

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.2.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.zy.starter</groupId>
	<artifactId>zy-spring-boot-starter-autoconfigurer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>zy-spring-boot-starter-autoconfigurer</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!-- 引入spring-boot-starter; 所有starter的基本配置 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<!--可以生成配置类提示文件-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

</project>
  • 2.创建配置类和自动配置类

配置类

/**
 * Description:
 *
 * @author zygui
 * @date 2020/4/19 11:00
 */
@ConfigurationProperties(prefix = "gzy")
public class GzyProperties {

    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

自动配置类

/**
 * Description: 自动配置类
 *
 * @author zygui
 * @date 2020/4/19 11:05
 */
@Configuration
@ConditionalOnWebApplication // web应用才生效
@EnableConfigurationProperties(GzyProperties.class) //让配置类生效,(注入到容器中)
public class GzyAutoConfiguration {

    @Autowired
    private GzyProperties gzyProperties;

    @Bean
    public HelloService helloService() {
        HelloService service = new HelloService();
        service.setGzyProperties(gzyProperties);
        return service;
    }
}

HelloService类

public class HelloService {

    GzyProperties gzyProperties;

    public GzyProperties getGzyProperties() {
        return gzyProperties;
    }

    public void setGzyProperties(GzyProperties gzyProperties) {
        this.gzyProperties = gzyProperties;
    }

    public String sayHelloGzy(String name) {
        return gzyProperties.getPrefix() + "-" + name + gzyProperties.getSuffix();
    }
}

这里也可以省略HelloService, 将其作为自动配置类的一个内部类, SpringBoot源码大多也是这样写的; 具体看下面
配置类不变

自动配置类

/**
 * Description: 自动配置类
 *
 * @author zygui
 * @date 2020/4/19 11:05
 */
@Configuration
@ConditionalOnWebApplication // web应用才生效
@EnableConfigurationProperties(GzyProperties.class) //让配置类生效,(注入到容器中)
public class GzyAutoConfiguration {

    private final GzyProperties gzyProperties;

    /**
     * 通过构造器注入
     *
     * @param gzyProperties
     */
    public GzyAutoConfiguration(GzyProperties gzyProperties) {
        this.gzyProperties = gzyProperties;
    }

    @Bean
    public HelloService helloService() {
        return new HelloService();
    }

    public class HelloService {

        public String sayHelloGzy(String name) {
            return gzyProperties.getPrefix() + "-" + name + "-" + gzyProperties.getSuffix();
        }
    }
}
  • 3.在resources文件夹下创建META-INF/spring.factories
    在这里插入图片描述

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.clboy.spring.boot.autoconfigure.ClboyAutoConfiguration
  • 4.安装到本地仓库
    在这里插入图片描述

2、创建自定义启动器

5.创建starter,选择maven工程即可,只是用于管理依赖,添加对AutoConfiguration模块的依赖

<?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.zy</groupId>
    <artifactId>zy-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 启动器 -->
    <dependencies>
        <!-- 在自定义的启动器中, 引入自定义的自动配置模块 -->
        <dependency>
            <groupId>com.zy.starter</groupId>
            <artifactId>zy-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>
  • 6.安装到本地仓库
    在这里插入图片描述

3、创建项目测试,选择添加web场景,因为设置是web场景才生效

7.引入自定义的启动器

<?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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zy</groupId>
    <artifactId>springboot-09-starter-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-09-starter-test</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-web</artifactId>
        </dependency>

        <!-- 引入自定义的starter -->
        <dependency>
            <groupId>com.zy</groupId>
            <artifactId>zy-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在这里插入图片描述

  • 8.创建Controller
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHelloGzy("GZY");
    }
}
  • 9.在application.properties配置文件中可以配置
gzy.prefix=HELLO
gzy.suffix=WORLD

也可以搜索GzyProperties可以看到自定义XxxProperties

  • 10.测试
    在这里插入图片描述

调用过程 :
在这里插入图片描述

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