Spring Boot Starter

本文介绍如何在Spring Boot项目中创建自定义Starter,包括配置自动化、参数映射实体及条件判断注解等内容。

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

传统Maven项目中通常将一些层、组件拆分为模块来管理,以便相互依赖复用。在Spring Boot项目中我们则可以创建自定义Spring Boot Starter来达成该目的。


配置pom

自定义starter并且通过spring-boot-autoconfigure完成自动化配置。
在pom.xml中配置对spring-boot-autoconfigure的依赖,如果继承了spring-boot-starter-parent,则可以不配置。

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-autoconfigure</artifactId>
 </dependency>

配置映射参数实体

Spring Boot提供了一个注解@ConfigurationProperties,该注解可以完成将application.properties配置文件内的有规则的配置参数映射到实体内的field内,实体需要提供setter方法。

import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "前缀")
public class HelloProperties{
    //消息内容
    private String msg = "default message";
}


实现自动化配置

自动化配置提供实体bean的验证以及初始化。

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//开启配置
@EnableConfigurationProperties(HelloProperties.class)//开启使用映射实体对象
//初始化该配置类的多个条件注解
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(
                prefix = "hello",//存在配置前缀hello
                value = "enabled",//开启
                matchIfMissing = true//缺失检查
)
public class HelloAutoConfiguration{
    @Autowired
    private HelloProperties helloProperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setMsg(helloProperties.getMsg());//设置消息内容

        return helloService;
    }
}

指明自动化装配位置

在注解@SpringBootApplication中已经存在一个开启自动化配置的注解@EnableAutoConfiguration来完成自动化配置,其内部使用了EnableAutoConfigurationImportSelector,继而使用了SpringFactoriesLoader.loadFactoryNames方法进行扫描具有META-INF/spring.factories文件的jar包。

我们在src/main/resource目录下创建META-INF目录,新建文件spring.factories,写入如下内容:

#配置自定义Starter的自动化配置,多个值可用逗号分隔。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxx.HelloAutoConfiguration


@Conditional 条件判断注解

Spring 4.X 新加入了@Conditional注解,控制某些configuration是否生效。除了自己自定义Condition,Spring还提供了很多ConditionalOn注解。这些注解里的条件可以是多个,可以赋默认值,也可以标注在类上。如果标注在类上,则对类里的所有@Bean方法都生效。

@ConditionalOnProperty(value = {"abc.property"}, matchIfMissing = false) #其name属性用来从application.properties中读取某个属性值,如果该值为空,则返回false。如果值不为空,则将该值与havingValue属性指定的值进行比较返回true或false。

开发中热部署

LiveReload Server,spring-boot-devtools

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值