Before the order

I have written two articles about the principle of SpringBoot automatic assembly analysis, interested partners can reviewCopy the code

Analysis of SpringBoot autowiring principles (Chapter 1) using Spring. factories files for cross-module instantiation

ConditionalOnBean (@conditionalonxxx

The last article we interesting, to engage in hands-on combat, their own hands to write a middleware starter, to experience SpringBoot automatic assemblyCopy the code

purpose

Start to write a middleware springboot-Myredis-starter project, built-in RedisUtil tool class method, business-oriented to screen out the tedious configuration and connection of the gaudy method, out of the box, Muggle API for business only need three steps: 3 @autoWired private RedisUtil RedisUtil; Use the Muggle API directlyCopy the code

Springboot-myredis-starter Setup process

1 Create springboot-Mybatis -starter middleware project 2 write @enableonredis configuration classCopy the code
/** * @target ({elementtype.type}) @Retention(RetentionPolicy.runtime) @Documented Public @interface EnableOnRedis { }Copy the code

Write the RedisUtil utility classCopy the code
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); }}Copy the code

ConditionOnBean poM package and EnableRedis classCopy the code
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.14.RELEASE</version> </dependency> /** * Use this annotation * to enable this configuration only when EnableRedis annotation exists in the Spring container. ConditionalOnBean(annotation = EnableOnRedis. Class) @component public class EnableRedis {// ConditionalOnBean(annotation = EnableOnRedis. Class) @component public class EnableRedis {// ConditionalOnBean(annotation = EnableOnRedis RedisUtil getRedisUtil() { return new RedisUtil(); }}Copy the code

5 Create the META_INF/spring.factories file and write the EnableRedis configuration to the fileCopy the code
# configuration need beans across the module information org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration = com. Mytest. Starter. Util. EnableRedisCopy the code

Springboot-myredis-starter function verification

Springboot-myredis-starter starter starter starter starter starter starter starter starter starter starterCopy the code

2 Startup configuration @enableonredis Indicates that the springboot-Myredis -starter function of the middleware is enabledCopy the code
@springbootApplication // test springboot-myredis-starter @enableonredis public class SpringEasyMain {public static void main(String[] args) { SpringApplication.run(SpringEasyMain.class); }}Copy the code

@autoWired private RedisUtil RedisUtil; Verify that it is null and availableCopy the code
/** * Springboot -myredis-starter */ @restController@requestMapping ("/myredis") public class MyRedisTestController { @Autowired private RedisUtil redisUtil; @GetMapping("/one") public void testOne() { redisUtil.set("1", "test1"); }}Copy the code

4 Test verification. The verification is successfulCopy the code

Verify that the @enableonredis annotation is added to the startup class. Is RedisUtil valid? The RedisUtil Bean is missingCopy the code

The RedisUtil Bean is not in use. The RedisUtil Bean is not in useCopy the code

Combined with SpringBoot auto assembly summary

The SpringFactoriesLoader loads the spring-.factories configuration that is required Information for cross-module beans, Is also tell Spring this configuration need to be ready to load org. Springframework. Boot. Autoconfigure. EnableAutoConfiguration = com. Mytest. Starter. Util. EnableRedis 2 ConditionalOnBean(annotation = enableonredis.class) to actually enable this plugin configuration The Spring container must have an EnableOnRedis annotation, which is the EnableOnRedis annotation on the launch class, @conditionalonBean (Annotation = EnableOnRedis. Class) is filtered and Spring can load the EnableRedis Bean. Spring loads the EnableRedis and RedisUtil utility 4 for the business side according to the @bean method ******, out of the box @autoWired private RedisUtil RedisUtil;Copy the code

Download the source code

Springboot – Myredis -starter source download