自定义Spring Boot Starter

1. 新建my-spring-boot-starter项目

完整的目录结构如下:

在这里插入图片描述

2. 引入相关依赖

pom.xml

<properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.2.2.RELEASE</spring-boot.version>
    </properties>

    <!-- 版本依赖集中管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <!-- 引入相关依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </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>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.liuliu.starter.MySpringBootStarterApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3.自定义服务类

MyStarterService.java

// 自定义服务类,供后续其他模块使用
public class MyStarterService {
    private String message;

    public MyStarterService(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

4.自定义属性配置文件类

MyStarterProperties.java

// 属性配置类,把配置文件的属性通过k-v的形式绑定到当前bean的属性中
@Configuration
@ConfigurationProperties(prefix = "my.starter")
public class MyStarterProperties {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

5.自定义Spring Boot启动自动配置类

MyStarterAutoConfiguration.java

/**
 * MyStarter 自动配置类。
 * 
 * 该类用于自动配置 MyStarter 的相关 Bean,当满足特定条件时会自动创建。
 */
@Configuration
@EnableConfigurationProperties(MyStarterProperties.class) // 启用 MyStarterProperties 属性绑定
public class MyStarterAutoConfiguration {

    /**
     * 创建 MyStarterService 的 Bean。
     * 
     * 当上下文中不存在 MyStarterService 的 Bean 时,会创建一个新的 MyStarterService 实例。
     * 
     * @param myStarterProperties MyStarterProperties 的配置类,提供必要的配置参数
     * @return MyStarterService 实例
     */
    @ConditionalOnMissingBean(MyStarterService.class) // 仅在没有 MyStarterService Bean 时创建
    @Bean // 声明该方法返回的对象是一个 Bean
    public MyStarterService myStarterService(MyStarterProperties myStarterProperties) {
        // 使用配置属性中的消息创建并返回 MyStarterService 实例
        return new MyStarterService(myStarterProperties.getMessage());
    }
}

6.创建spring.factories文件

Resources/META-INF目标下创建spring.factories文件,指定SpringBoot启动时自动配置类的全路径。

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.liuliu.starter.config.MyStarterAutoConfiguration

7.项目打包

使用maven命令:

mvn clean install

或者idea直接install打包:

在这里插入图片描述

打包完成后,在target目录下生成:

在这里插入图片描述

8.在其他项目中使用my-spring-boot-starter

1.引入my-spring-boot-starter坐标

<dependency>
   <groupId>com.liuliu.starter</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2.在application.yml文件中编辑

application.yml文件中编辑,添加如下内容,下面的内容将会和my-spring-boot-starter中的MyStarterProperties属性配置类进行绑定。

my:
  starter:
    message: "Hello from My Spring Boot Starter!"

3.测试类测试

    @Autowired
    private MyStarterService myService;

    @Test
    void test1() {
        System.out.println(myService.getMessage());
    }

然后控制台输出:

Hello from My Spring Boot Starter!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值