创建自己的SpringBoot-starter

本文详细介绍了如何使用Spring Boot创建自定义Starter,包括Maven工程搭建、依赖引入、自动配置类实现、属性读取及测试步骤。通过具体示例,展示了如何在其他项目中引入并使用自定义Starter。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用IDEA创建一个maven工程

2、引入依赖后,最终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>com.beck</groupId>
    <artifactId>beck-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.0.RELEASE</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>

3、创建需要自动配置的类(StreamYearService.java)

package com.beck.service;

public class StreamYearService {
    private String configName;

    public StreamYearService(String name){
        this.configName = name;
    }

    public void printStreamYear(){
        System.out.println("streamyear-spring-boot-starter ::: " + configName);
    }
}

4、创建读取配置的类(StreamYearServiceProperties.java)

package com.beck.service;

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

@ConfigurationProperties("streamyear.service")
public class StreamYearServiceProperties {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

5、创建自动配置的类(StreamYearServiceAutoConfigure .java)

package com.beck.service;

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

@Configuration
@ConditionalOnClass(StreamYearService.class)
@EnableConfigurationProperties(StreamYearServiceProperties.class)
public class StreamYearServiceAutoConfigure {
    @Autowired
    private StreamYearServiceProperties streamYearServiceProperties;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "streamyear.service",value = "enabled", havingValue = "true")
    public StreamYearService streamYearService(){
        return new StreamYearService(streamYearServiceProperties.getName());
    }

}

6、在resources目录下创建目录META-INF,在META-INF里面创建spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.beck.service.StreamYearServiceAutoConfigure

8、测试:在其他项目中引入我们自己的starter

<dependency>
            <groupId>com.beck</groupId>
            <artifactId>beck-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

9、在测试的项目配置文件中加入

streamyear.service.enabled=true
streamyear.service.name= = 我哦哦哦哦!!!

10、在项目的controller代码中加入如下代码

@Autowired
private StreamYearService streamYearService;

@RequestMapping("starter")
public void starter(){
    streamYearService.printStreamYear();
}

11、测试(请求:http://localhost:8088/api/student/starter)

12、最终执行结果

streamyear-spring-boot-starter ::: = 我哦哦哦哦!!!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值