springboot中自定义starter
springboot中的starter
- SpringBoot中的Starter其实也是一种容器,里面包含了诸多依赖。例如Spring-boot-starter-data-mongodb中就包含了五种依赖:Spring-boot-starter,MongoDB Java驱动,Spring-core,spring-tx,spring-data-mongodb。引入Spring-boot-starter-data-mongodb就不需要引入以上五种依赖,就像一个封装好的mongodb操作类库,可以直接对mongodb进行操作使用。
- Spring Boot有一个“约定大于配置”的规则,让程序组件之间来减少配置,降低复杂性。

@Configuration //指定这个类是一个配置类
@ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
@AutoConfigureAfter //指定自动配置类的顺序
@Bean //给容器中添加组件
@ConfigurationPropertie//将配置文件如(application.yml)中的配置属性,根据匹配的前缀与相关xxxProperties类进行绑定
@EnableConfigurationProperties//让xxxProperties生效加入到容器中
- starter命名规约:
- 官方命名空间
- 前缀:“spring-boot-starter-”
- 模式:spring-boot-starter-模块名
- 举例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
- 自定义命名空间
- 后缀:“-spring-boot-starter”
- 模式:模块-spring-boot-starter
- 举例:mybatis-spring-boot-starter
如何将一个模块功能自定义为一个starter
创建一个starter项目,关于项目的命名参考上述命名规约

- 启动器模块是一个空 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动
装配或者其他类库。启动器模块依赖自动配置模块;别人只需要引入启动器(starter)即可使用; - 专门构造一个自动配置模块,主要用来定义配置参数、以及自动配置对外暴露的功能(一般是抽象的接口Spring Bean)。
自动配置模块(autoconfigure)
- 创建对应的配置类
- @ConfigurationPropertie//将配置文件如(application.yml)中的配置属性,根据匹配的前缀与相关xxxProperties类进行绑定
- @EnableConfigurationProperties//让xxxProperties生效加入到容器中
@ConfigurationProperties(prefix = SmsProperties.SMS_PREFIX)
@Data
public class SmsProperties {
static final String SMS_PREFIX = "sms";
private Aliyun aliyun;
private boolean enabled;
@Data
public static class Aliyun {
private String signName;
private String accessKeyId;
private String accessKeySecret;
}
}
//对应application.yml文件中的配置参数如下
sms:
enabled: true
aliyun:
access-key-id: test
access-key-secret: test
sign-name: test
- 配置自动暴露功能接口(配置bean)
- @Configuration //指定这个类是一个配置类
- @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效
- @AutoConfigureAfter //指定自动配置类的顺序
- @Bean //给容器中添加组件
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "sms.aliyun",name = "sign-name")
@ConditionalOnClass(IAcsClient.class)
public class AliyunSMSConfiguration {
/**
* Sms sender sms sender.
*
* @param smsProperties the sms properties
* @return the sms sender
*/
@Bean
public SmsSender aliyunSmsSender(SmsProperties smsProperties) {
SmsProperties.Aliyun aliyun = smsProperties.getAliyun();
return new AliSmsSender(smsProperties.isEnabled(), aliyun.getSignName(), aliyun.getAccessKeyId(), aliyun.getAccessKeySecret());
}
}
- starter集成入应用有两种方式。我们从应用视角来看有两种:
- 一种是主动生效,在starter组件集成入Spring Boot应用时需要你主动声明启用该starter才生效,即使你配置完全。这里会用到@Import注解,将该注解标记到你自定义的@Enable注解上。最后,我们将该注解标记入SpringBoot应用启动类上就可以使用相应功能了。
/**
* 启用短信息配置
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AliyunSMSConfiguration.class)
public @interface EnableSMS {
}
- 另一种被动生效,在starter组件集成入Spring Boot应用时就已经被应用捕捉到。在autoconfigure资源包下新建META-INF/spring.factories写AliyunSMSConfiguration全限定名。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.jp.boot.msg.autoconfigure.AliyunSMSConfiguration
多个配置类逗号隔开,换行使用反斜杠。
starter模块
- 该模块是一个空jar。它唯一目的是提供必要的依赖项
无论哪种方式,您的starter必须直接或间接引用核心Spring Boot启动器(spring-boot-starter)(如果您的启动器依赖于另一个启动器,则无需添加它)。如果只使用自定义启动器创建项目,则Spring Boot的核心功能将通过核心启动器来实现。
我们的starter模块仅仅是以下的pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>cn.jp.boot</groupId>
<artifactId>sms-spring-boot</artifactId>
<version>1.0.0.RELEASE</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>cn.jp.boot</groupId>
<artifactId>sms-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
</dependencies>
版权声明:本文为ab1024249403原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。