Spring Boot (一): Spring Boot starter自定义

2023-07-29,,

前些日子在公司接触了spring bootspring cloud,有感于其大大简化了spring的配置过程,十分方便使用者快速构建项目,而且拥有丰富的starter供开发者使用。但是由于其自动化配置的原因,往往导致出现问题,新手无法快速定位问题。这里我就来总结一下spring boot 自定义starter的过程,相信大家看完这篇文章之后,能够对spring boot starter的运行原理有了基本的认识。
 为了节约你的时间,本篇文章的主要内容有:

spring boot starter的自定义
spring boot auto-configuration的两种方式,spring.factories和注解
Conditional注解的使用

引入pom依赖

相信接触过spring boot的开发者都会被其丰富的starter所吸引,如果你想给项目添加redis支持,你就可以直接引用spring-boot-starter-redis,如果你想使项目微服务化,你可以直接使用spring-cloud-starter-eureka。这些都是spring boot所提供的便利开发者的组件,大家也可以自定义自己的starter并开源出去供开发者使用。
 创建自己的starter项目需要maven依赖是如下所示:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.4.4.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>

核心配置类StorageAutoConfigure

构建starter的关键是编写一个装配类,这个类可以提供该starter核心bean。这里我们的starter提供一个类似redis的键值存储功能的bean,我们叫它为StorageService。负责对这个bean进行自动化装配的类叫做StorageAutoConfigure。保存application.properties配置信息的类叫做StorageServiceProperties。这三种类像是铁三角一样,你可以在很多的spring-boot-starter中看到他们的身影。
 我们首先来看StorageAutoConfigure的定义。

@Configuration
@ConditionalOnClass(StorageService.class)
@EnableConfigurationProperties(StorageServiceProperties.class)
public class StorageAutoConfigure {
@Autowired
private StorageServiceProperties properties; @Bean
@ConditionalOnMissingBean(StorageService.class)
@ConditionalOnProperty(prefix = "storage.service", value = "enabled", havingValue = "true")
StorageService exampleService() {
return new StorageService(properties);
}
}

我们首先讲一下源码中注解的作用。

@Configuration,被该注解注释的类会提供一个或则多个@bean修饰的方法并且会被spring容器处理来生成bean definitions
@bean注解是必须修饰函数的,该函数可以提供一个bean。而且该函数的函数名必须和bean的名称一致,除了首字母不需要大写。
@ConditionalOnClass注解是条件判断的注解,表示对应的类在classpath目录下存在时,才会去解析对应的配置文件。
@EnableConfigurationProperties注解给出了该配置类所需要的配置信息类,也就是StorageServiceProperties类,这样spring容器才会去读取配置信息到StorageServiceProperties对象中。
@ConditionalOnMissingBean注解也是条件判断的注解,表示如果不存在对应的bean条件才成立,这里就表示如果已经有StorageService的bean了,那么就不再进行该bean的生成。这个注解十分重要,涉及到默认配置和用户自定义配置的原理。也就是说用户可以自定义一个StorageService的bean,这样的话,spring容器就不需要再初始化这个默认的bean了。
ConditionalOnProperty注解是条件判断的注解,表示如果配置文件中的响应配置项数值为true,才会对该bean进行初始化。

看到这里,大家大概都明白了StorageAutoConfigure的作用了吧,spring容器会读取相应的配置信息到StorageServiceProperties中,然后依据调节判断初始化StorageService这个bean。集成了该starter的项目就可以直接使用StorageService来存储键值信息了。

配置信息类StorageServiceProperties

存储配置信息的类StorageServiceProperties很简单,源码如下所示:

@ConfigurationProperties("storage.service")
public class StorageServiceProperties {
private String username;
private String password;
private String url; ......
//一系列的getter和setter函数
}

@ConfigurationProperties注解就是让spring容器知道该配置类的配置项前缀是什么,上述的源码给出的配置信息项有storage.service.username,storage.service.passwordstorage.service.url,类似于数据库的host和用户名密码。这些配置信息都会由spring容器从application.properties文件中读取出来设置到该类中。

starter提供功能的StorageService

StorageService类是提供整个starter的核心功能的类,也就是提供键值存储的功能。

public class StorageService {
private Logger logger = LoggerFactory.getLogger(StorageService.class);
private String url;
private String username;
private String password;
private HashMap<String, Object> storage = new HashMap<String, Object>();
public StorageService(StorageServiceProperties properties) {
super();
this.url = properties.getUrl();
this.username = properties.getUsername();
this.password = properties.getPassword();
logger.debug("init storage with url " + url + " name: " + username + " password: " + password);
} public void put(String key, Object val) {
storage.put(key, val);
} public Object get(String key) {
return storage.get(key);
}
}

注解配置和spring.factories

 自定义的starter有两种方式来通知spring容器导入自己的auto-configuration类,也就是本文当中的StorageAutoConfigure类。
 一般都是在starter项目的resources/META-INF文件夹下的spring.factories文件中加入需要自动化配置类的全限定名称。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=starter.StorageAutoConfigure

spring boot项目中的EnableAutoConfigurationImportSelector会自动去每个jar的相应文件下查看spring.factories文件内容,并将其中的类加载出来在auto-configuration过程中进行配置。而EnableAutoConfigurationImportSelector@EnableAutoConfiguration注解中被import
 第一种方法只要是引入该starter,那么spring.factories中的auto-configuration类就会被装载,但是如果你希望有更加灵活的方式,那么就使用自定义注解来引入装配类。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(StorageAutoConfigure.class)
@Documented
public @interface EnableStorage {
}

 有了这个注解,你可以在你引入该starter的项目中使用该注解,通过@import注解,spring容器会自动加载StorageAutoConfigure并自动化进行配置。

后记

作者:ztelur
链接:https://www.jianshu.com/p/4735fe7ae921
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
==
 
https://juejin.im/entry/58d37630570c350058c2c15c

Spring Boot (一): Spring Boot starter自定义的相关教程结束。

《Spring Boot (一): Spring Boot starter自定义.doc》

下载本文的Word格式文档,以方便收藏与打印。