Spring Boot 必备技能之Starter自定义

本文摘自于《Spring Cloud微服务 入门 实战与进阶》一书。

Spring Boot的方便体现在简化了很多繁琐的配置,对开发人员来说是一个福音,通过引入各种Spring Boot Starter包可以快速的搭建出一个项目的脚手架。

目前提供的Spring Boot Starter包有:

  • spring-boot-starter-web:快速构建基于Spring MVC的Web项目,使用Tomcat做默认嵌入式容器。

  • spring-boot-starter-data-redis:操作Redis。

  • spring-boot-starter-data-mongodb:操作Mongodb。

  • spring-boot-starter-data-jpa:操作Mysql。

  • spring-boot-starter-activemq:操作Activemq。

  • 等等......

自动配置非常方便,当我们要操作Mongodb的时候,只需要引入spring-boot-starter-data-mongodb的依赖,然后配置Mongodb的链接信息 spring.data.mongodb.uri=mongodb://localhost/test就可以使用MongoTemplate来操作数据,MongoTemplate的初始化工作全部交给Starter来完成。

自动配置麻烦的是当出现错误时,排查问题的难度上升了。自动配置的逻辑都在Spring Boot Starter中,要快速的能够定位问题,那么你必须得了解Spring Boot Starter的内部原理。接下来我们自己动手来实现一个Spring Boot Starter。

开发Starter步骤:

  • 创建Starter项目

  • 定义Starter需要的配置(Properties)类

  • 编写自动配置类

  • 编写spring.factories文件加载自动配置类

  • 编写配置提示文件spring-configuration-metadata.json(不是必须的)

创建一个项目spring-boot-starter-demo,Pom.xml配置如下:


   
   
  1. <dependencies>

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-web</artifactId>

  5. </dependency>

  6. <dependency>

  7. <groupId>org.projectlombok</groupId>

  8. <artifactId>lombok</artifactId>

  9. <optional>true</optional>

  10. </dependency>

  11. </dependencies>

创建一个配置类,用于在属性文件中配置值,相当于spring.data.mongo这种形式


   
   
  1. import org.springframework.boot.context.properties.ConfigurationProperties;

  2. import lombok.Data;

  3. @Data

  4. @ConfigurationProperties("spring.user")

  5. public class UserPorperties {

  6. private String name;

  7. }

@ConfigurationProperties指定了配置的前缀,也就是spring.user.name=XXX

再定义一个Client,相当于MongoTemplate,里面定一个方法,用于获取配置中的值


   
   
  1. public class UserClient {

  2. private UserPorperties userPorperties;

  3. public UserClient() {

  4. }

  5. public UserClient(UserPorperties p) {

  6. this.userPorperties = p;

  7. }

  8. public String getName() {

  9. return userPorperties.getName();

  10. }

  11. }

一个最基本的Starter包定义好了,但目前肯定是不能使用UserClient ,因为我们没有去自动构建UserClient 的实例,接下来开始构建UserClient


   
   
  1. @Configuration

  2. @EnableConfigurationProperties(UserPorperties.class)

  3. public class UserAutoConfigure {

  4. @Bean

  5. @ConditionalOnProperty(prefix = "spring.user",value = "enabled",havingValue = "true")

  6. public UserClient userClient(UserPorperties userPorperties) {

  7. return new UserClient(userPorperties);

  8. }

  9. }

Spring Boot会默认扫描跟启动类平级的包,如果我们的Starter跟启动类不在同一个主包下,如何让UserAutoConfigure 生效?

第一种方式:

在resources下创建一个META-INF文件夹,然后在META-INF文件夹中创建一个spring.factories文件,文件中指定自动配置的类


   
   
  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

  2. com.cxytiandi.demo.UserAutoConfigure

Spring Boot启动时会去读取spring.factories文件,然后根据配置激活对应的配置类,到底为止就简单的实现了一个Starter包。

现在可以在其他的项目中引入这个Starter包:


   
   
  1. <dependency>

  2. <groupId>com.example</groupId>

  3. <artifactId>spring-boot-starter-demo</artifactId>

  4. <version>0.0.1-SNAPSHOT</version>

  5. </dependency>

引入之后就直接可以使用UserClient,UserClient 在项目启动的时候已经自动初始化好。


   
   
  1. @RestController

  2. public class UserController {

  3. @Autowired

  4. private UserClient userClient;

  5. @GetMapping("/user/name")

  6. public String getUserName() {

  7. return userClient.getName();

  8. }

  9. }

很多时候我们不想引入了Starter包就执行初始化的逻辑,想要用户来指定是否要开启Starter包的自动配置功能,比如常用的@EnableAsync这个注解就是用于开启调用方法异步执行的功能。

同样的我们也可以通过注解的方式来开启是否自动配置,如果用注解的方式,那么spring.factories就不需要编写了,下面来看怎么定义启用自动配置的注解。


   
   
  1. @Target({ElementType.TYPE})

  2. @Retention(RetentionPolicy.RUNTIME)

  3. @Documented

  4. @Inherited

  5. @Import({UserAutoConfigure.class})

  6. public @interface EnableUserClient {

  7. }

核心是@Import({UserAutoConfigure.class})这行代码,通过导入的方式实现把UserAutoConfigure实例加入SpringIOC容器中,这样就能开启自动配置了。

使用方式就是在启动类上加上该注解,代码入下:


   
   
  1. @EnableUserClient

  2. @SpringBootApplication

  3. public class SpringBootDemoApplication {

  4. public static void main(String[] args) {

  5. SpringApplication.run(SpringBootDemoApplication.class, args);

  6. }

  7. }

在某些场景下,UserAutoConfigure中会配置多个对象,对于这些对象,不想全部配置,也想让用户指定需要开启配置的时候再去构建对象,这个时候我们可以通过@ConditionalOnProperty来指定是否开启配置的功能,代码如下:


   
   
  1. @Bean

  2. @ConditionalOnProperty(prefix = "spring.user",value = "enabled",havingValue = "true")

  3. public UserClient userClient(UserPorperties userPorperties) {

  4. return new UserClient(userPorperties);

  5. }

通过上面的配置,只有当启动类加了@EnableUserClient并且配置文件中spring.user.enabled=true的时候才会自动配置UserClient 。

在自定义Starter包的过程中,还有一点也比较重要,就是需要对配置的内容项进行提示,需要注意的是Eclipse中是不支持提示的,我用的Spring Tools 4 for Eclipse,如下图:

定义提示内容需要在META-INF中创建一个spring-configuration-metadata.json


   
   
  1. {

  2. "properties": [

  3. {

  4. "name": "spring.user.name",

  5. "defaultValue": "cxytinadi"

  6. },

  7. {

  8. "name": "spring.user.enabled",

  9. "type": "java.lang.Boolean",

  10. "defaultValue": false

  11. }

  12. ]

  13. }

  • name:配置名

  • type:配置的数据类型

  • defaultValue:默认值

本文摘自于《Spring Cloud微服务 入门 实战与进阶》一书。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值