Spring Boot 自定义 starter 功能

本文详细介绍了如何创建一个自定义的Spring Boot Starter,包括创建空工程、设置Maven模块、编写自动配置类、配置spring.factories文件,以及安装到本地仓库,并在新的Spring Boot项目中测试使用。通过这个过程,你可以理解Starter如何工作并实现自己的依赖导入功能。

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

1,创建一个空工程

2,在new Module创建一个maven工程

3,再new Module创建一个Spring Initializr

4,点击Apply,OK

5,在my-spring-boot-starter的pom.xml文件中引入myproject-spring-boot-starter-autoconfig自动配置包

    <groupId>com.myproject.starter</groupId>
    <artifactId>myproject-spring-boot-starter-autoconfig</artifactId>
    <version>0.0.1-SNAPSHOT</version>
<?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.myproject.starter</groupId>
    <artifactId>my-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--启动器-->
    <dependencies>
        <!--引入自动配置模块-->
        <dependency>
             <groupId>com.myproject.starter</groupId>
             <artifactId>myproject-spring-boot-starter-autoconfig</artifactId>
             <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

6,删掉没用的目录结构,及主程序启动类,删掉test目录,test依赖及插件;就引入starter依赖

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.21.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.myproject.starter</groupId>
    <artifactId>myproject-spring-boot-starter-autoconfig</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myproject-spring-boot-starter-autoconfig</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>
    </dependencies>

</project>

7,编写测试用的配置类

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

    private String prefix;
    private String suffix;

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

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

    public String getSuffix() {
        return suffix;
    }

    public String getPrefix() {
        return prefix;
    }
}
public class HelloService {


    HelloProperties helloProperties;

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

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

}
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {

    @Autowired
    HelloProperties helloProperties;

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

8,要让配置类启动生效需要在类路径下创建文件夹及配置文件:META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.myproject.starter.HelloAutoConfiguration

9,点击IDEA右侧的Maven Projects,将自定义完成的starter安装到仓库中,由于my-spring-boot-starter依赖的myproject-spring-boot-starter-autoconfig;所以先将myproject-spring-boot-starter-autoconfig安装

10,安装成功后,新创建一个Spring Boot项目,将web组件勾上,测试starter启动是否正常运行:

11,在pom文件中引入自定义的starter

        <dependency>
            <groupId>com.myproject.starter</groupId>
            <artifactId>myproject-spring-boot-starter-autoconfig</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

12,编写一个测试的Controller

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return helloService.sayHello("boot");
    }

}

13,可以在.properties配置文件中配置-前后缀

myproject.hello.prefix=myProject
myproject.hello.suffix=Good

14,然后启动应用,访问:localhost:8080/hello

OK!没问题!

 

总结:

1,创建一个启动器,只用来做依赖导入

2,写一个自动配置模块

@Configuration  //指定这个类是一个配置类
@ConditionalOnXXX  //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter  //指定自动配置类的顺序
@Bean  //给容器中添加组件

@ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置
@EnableConfigurationProperties //让xxxProperties生效加入到容器中

自动配置类要能加载
将需要启动就加载的自动配置类,配置在META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

3,启动器依赖自动配置,使用只需要引入启动器(starter)

Over!

可以结合前面的文章学习Spring Boot!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值