SpringBoot制作⼀个⾃⼰的Starter
  这⼏天在写⼀个团队使⽤的⼯具,有⼀些东西呢,需要从 Spring Cloud 配置中⼼中读取配置信息进⾏加载初始化,所以做了⼀个 Starter 。
pom 依赖
<properties>
<!-- Spring Boot -->
<spring.boot>1.5.9.RELEASE</spring.boot>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencys>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
适合新手的spring boot
</dependency>
</dependencys>
七⽜云服务
  这⾥提供了⼀个七⽜云客户端初始化的服务,伪代码如下:
public class QiniuService {
private static String AK;
private static String SK;
private static Auth AUTH;
private static Configuration CFG;
private static BucketManager BUCKET_MANAGER;
private static String BUCKET_NAME;
public QiniuService(String accesskey, String secretkey, String bucketName) {
init(accesskey, secretkey, bucketName);
}
/**
* 初始化七⽜云配置
*/
public static void init(String accesskey, String secretkey, String bucketName) {
if (StringUtils.isBlank(accesskey)
|| StringUtils.isBlank(secretkey)
|| StringUtils.isBlank(bucketName)) {
log.warn("【七⽜云服务】-初始化参数不⾜,不予初始化");
return;
}
try {
AK = accesskey;
SK = secretkey;
AUTH = ate(AK, SK);
CFG = new 0());
BUCKET_MANAGER = new BucketManager(AUTH, CFG);
BUCKET_NAME = bucketName;
} catch (Exception e) {
<("【七⽜云服务】-初始化出错:{}", StackTrace(e));
}
}
······
}
  这⾥啰嗦⼀下,官⽅对 Starter 包定义的 artifactId 是有要求的,当然也可以不遵守(⼿动滑稽)。Spring 官⽅ Starter 通常命名为spring-boot-starter-{name}如:spring-boot-starter-web,Spring 官⽅建议⾮官⽅的 Starter 命名应遵守 {name}-spring-boot-starter 的格式。
编写 properties 类
* @Description:配置中⼼配置信息导⼊到此类
* @CreateDate:9:21 2018/3/1
*/
@ConfigurationProperties()
public class QiNiuProperties {
/** 七⽜云相关 */
private String qiniuAccesskey;
private String qiniuSecretkey;
private String qiniuBucketName;
private String qiniuUrl;
public String getQiniuAccesskey() {
return qiniuAccesskey;
}
public void setQiniuAccesskey(String qiniuAccesskey) {
this.qiniuAccesskey = qiniuAccesskey;
QiniuService.init(qiniuAccesskey, qiniuSecretkey, qiniuBucketName);
}
public String getQiniuSecretkey() {
return qiniuSecretkey;
}
public void setQiniuSecretkey(String qiniuSecretkey) {
this.qiniuSecretkey = qiniuSecretkey;
QiniuService.init(qiniuAccesskey, qiniuSecretkey, qiniuBucketName);
}
public String getQiniuBucketName() {
return qiniuBucketName;
}
public void setQiniuBucketName(String qiniuBucketName) {
this.qiniuBucketName = qiniuBucketName;
QiniuService.init(qiniuAccesskey, qiniuSecretkey, qiniuBucketName);
}
public String getQiniuUrl() {
return qiniuUrl;
}
public void setQiniuUrl(String qiniuUrl) {
this.qiniuUrl = qiniuUrl;
}
}
重点,编写 AutoConfigure 类
* @Description:七⽜云服务⾃动配置类
* @CreateDate:10:42 2018/7/25
*/
@Configuration
@ConditionalOnClass(QiniuService.class)
@EnableConfigurationProperties(QiNiuProperties.class)
public class QiNiuAutoConfig {
@Resource
private QiNiuProperties qiNiuProperties;
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "qiniu", value = "enabled", havingValue = "true")
QiniuService qiniuService (){
return new QiniuAccesskey(), QiniuSecretkey(), QiniuBucketName());
}
}
  有⼏个注解这⾥记录⼀下:
@ConditionalOnClass:某个 Class 位于类路径上,才会实例化⼀个Bean。也就是说,当 classpath 下发现该类的情况下进⾏实例化。
@EnableConfigurationProperties:为带有 @ConfigurationProperties 注解的 Bean 提供有效的⽀持。这个注解可以提供⼀种⽅便的⽅式来将带有 @ConfigurationProperties 注解的类注⼊为 Spring 容器的 Bean。
@ConditionalOnMissingBean:当 Spring Context中不存在该Bean时。
@ConditionalOnProperty:我此处的意思为:当配置⽂件中 able 的值为 true 时,实例化此类。
建⽴ spring.factories ⽂件
  在 resources/META-INF/ 下创建 spring.factories ⽂件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
fig.qiniu.QiNiuAutoConfiguration, \
......
  可以配置多个⾃动配置类,以,分割。
打包
mvn clean install
引⽤
  只需把⾃⼰的 Starter 引⼊之后,在配置⽂件中配置加⼊七⽜云配置即可。
### 七⽜云相关
## 基础配置
# AK
qiniu-accesskey: 你的 accesskey
# SK
qiniu-secretkey: 你的 secretkey
# 芯球泊车app使⽤
qiniu-bucket-name: 你的 backet name

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。