likes
comments
collection
share

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件 springboot-myredis-stater

作者站长头像
站长
· 阅读数 29

前序

前面写过两篇关于SpringBoot自动装配原理的解析,有兴趣的小伙伴可以回顾下
最后一篇我们整点有意思的,搞一手实战,自己动手写一个中间件starter,来体会一下SpringBoot的自动装配

目的

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


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

springboot-myredis-starter 搭建过程

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

}

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

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);
    }
}

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

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();
    }
}

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

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

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

springboot-myredis-starter 功能验证

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

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

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

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

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");
    }
}

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

4 测试验证,验证成功

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

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

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

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

SpringBoot的自动装配原理解析(第三章)之 教你手写中间件  springboot-myredis-stater

结合SpringBoot自动装配总结

从我们自己手写的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;
 

源码下载

springboot-myredis-starter源码下载