目 录CONTENT

文章目录

SpringBoot自定义starters(7)

Eric
2022-02-15 / 0 评论 / 0 点赞 / 142 阅读 / 842 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-12-12,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

SpringBoot自定义starters

1 自定义starter的原理

  • 这个场景需要使用的依赖是什么?
  • 如何编写自动配置。
@Configuration  //指定这个类是配置
@ConditionalOnXxx //在指定条件成立的情况下自动配置类生成
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationProperties结合相关的XxxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让XxxProperties类生效,并加入到容器中    
    
自动配置类要能加载,需要将其配置到META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
...
  • 启动器(starter)是一个空的jar文件,仅仅提供辅助性依赖管理,这些依赖可能用于自动装配或其他类库。

启动器原理.png

2 自定义starter

2.1 使用IDEA新建一个空的工程spring-boot-starter

新建一个空的工程spring-boot-starter.png

2.2 新建open1024-spring-boot-starter-autoconfigurer工程

  • 导入相关jar的Maven坐标:
<?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.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>top.open1024.starter</groupId>
    <artifactId>sunxiaping-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1</version>
    <name>sunxiaping-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>14</java.version>
    </properties>

    <dependencies>
        <!--    引入spring-boot-starter   -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>


</project>
  • 新建HelloProperties.java
package top.open1024.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;

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

    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;
    }
}
  • 新建HelloService.java
package top.open1024.starter;

public class HelloService {

    private HelloProperties helloProperties;

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    public String sayHello(String name) {
        return helloProperties.getPrefix() + "-" + name + "-" + helloProperties.getSuffix();
    }

}
  • 新建HelloServiceAutoConfiguration.java
package top.open1024.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(value = HelloProperties.class)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }

}
  • 在resource目录下新建META-INF目录,并在其新建spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
top.open1024.starter.HelloServiceAutoConfiguration

2.3 新建open1024-spring-boot-starter工程

  • 新建相关jar包的Maven坐标:
<?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>top.open1024.starter</groupId>
    <artifactId>open1024-spring-boot-starter</artifactId>
    <version>1.0</version>

    <!--  启动器  -->
    <dependencies>
        <!--   引入自动配置模块     -->
        <dependency>
            <groupId>top.open1024.starter</groupId>
            <artifactId>open1024-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1</version>
        </dependency>
    </dependencies>

</project>
0

评论区