starter 自动注入组件,为用户省去组件引入、配置类、jar包冲突解决。starter一般都包含2个类:ConfigurationProperties和AutoConfiguration。

命名规则

由于SpringBoot官方本身就提供了很多Starter,为了区别那些是官方的,哪些是第三方的,所以SpringBoot官方提出:

第三方提供的Starter统一用 xxx-spring-boot-starter

而官方提供的Starter统一用 spring-boot-starter-xxx

手写starter

项目目录

1、pom.xml

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

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
        <spring-boot.version>2.4.6</spring-boot.version>
    </properties>

    <dependencies>
        <!-- 用来提示用的 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <!-- 禁止传递依赖 -->
            <optional>true</optional>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
    </dependencies>

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

</project>

2、属性配置类

@ConfigurationProperties(prefix = "yzh.hello")
@Data
public class HelloProperties {

    private String name = "default";

}

3、业务执行类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class HelloStarter {

    private HelloProperties helloProperties;

    public String hello() {
        return "welcome " + helloProperties.getName() + "!";
    }

}

4、自动装配类

@ConditionalOnClass(HelloStarter.class)
@EnableConfigurationProperties(HelloProperties.class)
@Configuration
public class HelloAutoConfiguration {

    @Bean
    public HelloStarter helloStarter(HelloProperties helloProperties) {
        return new HelloStarter(helloProperties);
    }

}

5、自动装配类被springboot识别的配置文件

在resource目录下,新建一个META-INF文件夹,在META-INF文件夹下新建一个spring.factories文件。

#声明配置类的全路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yang.HelloAutoConfiguration

  

6、使用 mvn install 打包

 

7、其它项目引入使用

引入:

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>yzh-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

 application.properties中的配置:

yzh.hello.name=gouwa

代码: 

@RestController
@RequestMapping("/starter")
@Slf4j
public class StarterController {

    @Resource
    private HelloStarter helloStarter;

    @GetMapping("/index")
    public String index() {
        return helloStarter.hello();
    }
}

 启动后,访问:http://localhost:8080/starter/index

项目GitHub地址:https://github.com/zihea/yzh-spring-boot-starter


版权声明:本文为yzh_1346983557原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/yzh_1346983557/article/details/122968574