SpringBoot's automatic assembly principle analysis (Chapter 3) teaches you to write middleware springboot-myredis-stater

prologue

前面写过两篇关于SpringBoot自动装配原理的解析,有兴趣的小伙伴可以回顾下
复制代码

Analysis of SpringBoot's autowiring principle (Chapter 1) Cross-module instantiation through spring.factories file

Analysis of SpringBoot's automatic assembly principle (Chapter 2) realizes bean on-demand loading through @ConditionalOnBean, @ConditionalOnXXX annotations

最后一篇我们整点有意思的,搞一手实战,自己动手写一个中间件starter,来体会一下SpringBoot的自动装配
复制代码

Purpose

 动手写一个中间件 springboot-myredis-starter项目,内置RedisUtil工具类方法,
 面向业务方屏蔽掉繁琐配置连接花里胡哨方法,开箱即用,麻瓜式API


 对于业务方只需要三步操作: 1 引入springboot-myredis-starter pom包
                       2 启动类加上@EnableOnRedis注解表示开启此中间件服务
                       3 @Autowired 
                         private RedisUtil redisUtil; 直接使用麻瓜API
                         
复制代码

springboot-myredis-starter building process

1 创建springboot-mybatis-starter 中间件项目
2 编写@EnableOnRedis配置类
复制代码
/**
 * 自定义注解,用于RedisUtil功能的开关
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableOnRedis {

}
复制代码

wecom-temp-2a33ad875efc924eccdda725af06f0e1.png

3 编写RedisUtil 工具类
复制代码
/**
 * 模拟RedisUtil 服务工具类
 */
public class RedisUtil {

    Map<String, String> map = new HashMap();
    public void set(String key, String value) {
        this.map.put(key, value);
    }

    public String get(String key) {
        return this.map.get(key);
    }
}
复制代码

wecom-temp-71b963f8b46199363cbd08857620dcf6.png

4 引入@ConditionOnBean pom包,并编写配置加载开关EnableRedis类
复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>1.5.14.RELEASE</version>
</dependency>
/**
 * 使用此注解
 * 表示当Spring容器中,有EnableRedis注解存在时, 才会去开启此配置,
 */
@ConditionalOnBean(annotation = EnableOnRedis.class)
@Component
public class EnableRedis {

    // 装载实例化工具类
    @Bean
    public RedisUtil getRedisUtil() {
        return new RedisUtil();
    }
}
复制代码

Enterprise WeChat screenshot_a61690de-75cd-41e2-9324-592e30c2193b.png

5 创建META_INF/spring.factories文件,并将EnableRedis的配置写入
复制代码
# 配置需要 跨模块Bean的信息
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mytest.starter.util.EnableRedis
复制代码

Enterprise WeChat screenshot_6e65b570-77de-4ac7-b43a-ed36e4b0753e.png

springboot-myredis-starter functional verification

 我们自己随便搞个springboot功能,引入springboot-myredis-starter看看 是否能达到开箱即用的效果
 1 随便搭建springboot项目,引入springboot-myredis-starter pom包
 
复制代码

wecom-temp-8c9f4568ca545f8c941c76d4d925a82f.png

 2 启动类配置@EnableOnRedis 表示开启中间件springboot-myredis-starter功能
复制代码
 @SpringBootApplication
//测试springboot-myredis-starter
@EnableOnRedis
public class SpringEasyMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringEasyMain.class);
    }
}
复制代码

Enterprise WeChat screenshot_2d652922-45ec-4eaa-9678-96f52f8bb75c.png

3 编写Controller 注入 @Autowired private RedisUtil redisUtil;
  验证其是否空指针,是否可用
复制代码
/**
 * springboot-myredis-starter 启动验证
 */
@RestController
@RequestMapping("/myredis")
public class MyRedisTestController {
    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/one")
    public void testOne() {
        redisUtil.set("1", "test1");
    }
}
复制代码

Enterprise WeChat screenshot_fd29c893-76cc-4332-bfdc-28aeb6f08065.png

4 测试验证,验证成功
复制代码

Enterprise WeChat screenshot_22a8144e-edc5-48e6-a7bc-a97fcf091a7e.png

5 验证启动类会加入@EnableOnRedis注解,RedisUtil是否生效呢?,会发现直接启动报错,缺少RedisUtil Bean
复制代码

Enterprise WeChat screenshot_42659e44-bcb9-4059-a9b0-c4e593786b32.png

6 验证spring.factories文件,移除配置,RedisUtil是否生效,会发现直接启动报错,缺少RedisUtil Bean
复制代码

Enterprise WeChat screenshot_13e79652-1c1f-4468-a70a-6dd26666a3fd.png

Combined with SpringBoot automatic assembly summary

从我们自己手写的springboot-myredis-starter中间件来看 SpringBoot自动装配过程

1 ***SpringFactoriesLoader加载了spring.factories的配置
也就是# 配置需要 跨模块Bean的信息,也就是告诉Spring此配置需要准备加载org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mytest.starter.util.EnableRedis

2 通过@ConditionalOnBean(annotation = EnableOnRedis.class) 这个注解进行配置开关
  想真正开启此插件配置
  需要Spring容器中必须要有EnableOnRedis注解,也就是启动类上加上EnableOnRedis注解

  启动类加上了EnableOnRedis注解,@ConditionalOnBean(annotation = EnableOnRedis.class) 筛选过了,Spring就可以加载EnableRedis Bean了

3 Spring装载配置EnableRedis,并根据@Bean 方法装载 RedisUtil工具

4 对于业务方 ******,开箱即用
 @Autowired
 private RedisUtil redisUtil;
 
复制代码

Source code download

springboot-myredis-starter source code download

Guess you like

Origin juejin.im/post/7086753727364726797