I’ll put the 0 out front

There is a very important mechanism in SpringBoot called starter, which is an important manifestation of the “convention over configuration” philosophy. The ability to integrate functionality into starter can be introduced and used in Maven projects without the need for cumbersome configuration (think of it as a bunch of XML files). In daily development, we often encounter some common modules independent of business (such as log processing, Redis tools, etc.). Adhering to the concept of not repeating the wheel, we can consider packaging similar modules into a starter and introduce them as needed. Use SpringBoot’s autowiring to register beans into the IOC container.

1. Pre-knowledge

Encapsulating a starter is not suitable for a zero-base starter. Before you know how to package a starter, there are at least a few things you need to know:

  1. Basic uses of SpringBoot (such as creating projects, writing configuration files, and configuring classes)
  2. If you can understand the principle of SpringBoot automatic assembly, it is better, do not know, do not affect the learning of this demo
  3. Understand the basics of Redis (in fact, this is not mandatory, just because this demo is a Redis tool class like starter, so it is better to have some basic)
  4. The basics of Maven are at least understanding the concepts of local and remote repositories.
  5. Learn about custom annotations
  6. Installed redis

2 Start writing starter

  1. Create a Maven project

  2. Import the SpringBoot parent dependency

    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 2.5.3 < / version > < relativePath / > < / parent >Copy the code
  3. Introduce Redis-related dependencies

    <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> The < spring. The boot. Version > 2.5.3 < / spring. The boot. Version > < / properties > < dependencies > <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${spring.boot.version}</version> </dependency> </dependencies>Copy the code
  4. Create the base package structure

  5. Create the RedisTemplateConfig class under the config package

    package com.study.redis.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * @author markcwg * @date 2021/8/10 3:48 PM */ @configuration public class RedisTemplateConfig {@bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); / / define the key way to serialize redisTemplate. SetKeySerializer (new StringRedisSerializer ()); / / define the value serialization redisTemplate. SetValueSerializer (new GenericJackson2JsonRedisSerializer ()); return redisTemplate; }}Copy the code
  6. Create the RedisHelper class in the Helper package

    package com.study.redis.helper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.util.Set; /** * @author markcwg * @date 2021/8/10 3:53 PM */ @component public class RedisHelper {@autoWired private RedisTemplate<String, String> redisTemplate; Public Boolean hasKey(String key) {return redistemplate.haskey (key); /** redistemplate.haskey (key); } public Set<String> keys(String pattern) {return redisTemplate.keys(pattern); } public void set(String key, String key, String key, String key, String key, String key, String key) String value) { redisTemplate.opsForValue().set(key, value); } public String get(String key) {return;} public String get(String key) {return redisTemplate.opsForValue().get(key); }}Copy the code
  7. Create CustomRedisAutoConfiguration under config package

    package com.study.redis.config; import com.study.redis.helper.RedisHelper; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; / * * * @ author markcwg * he * / afternoon @ @ date 2021/7/30 ConditionalOnWebApplication @ the Configuration (proxyBeanMethods = false)  @ComponentScan(basePackages = "com.study.redis.config") public class CustomRedisAutoConfiguration { @Bean public RedisHelper redisHelper() { return new RedisHelper(); }}Copy the code
  8. Create a LoadRedis annotation under the Annotations package

    package com.study.redis.annotations;
    ​
    import com.study.redis.config.CustomRedisAutoConfiguration;
    import org.springframework.context.annotation.Import;
    ​
    import java.lang.annotation.*;
    ​
    /**
     * @author markcwg
     * @date 2021/8/10 4:00 下午
     */
    @Target(ElementType.TYPE)
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Import(CustomRedisAutoConfiguration.class)
    public @interface LoadRedis {
    }
    Copy the code
  9. Use MVN Install to put the starter into your own Maven repository

  10. Determine whether maven’s local repository already has artifacts (this step can be ignored)

3 Principle Explanation

Here’s a quick explanation of how the starter works: By introducing CustomRedisAutoConfiguration LoadRedis annotation automatically configure class, RedisHelper registered into the IOC container, in this class encapsulates the commonly used method of redis in RedisHelper can extend itself

4 Use a customized starter

  1. Create a new SpringBoot project

  2. Introducing a custom starter

    <dependency>
                <groupId>com.study</groupId>
                <artifactId>starter-demo</artifactId>
                <version>0.1</version>
    </dependency>
    Copy the code
  3. Writing configuration files (this step requires redis installed)

    Server: port: 8082 Spring: redis: host: 127.0.0.1 port: 6379Copy the code
  4. Annotate the startup class with LoadRedis

    @SpringBootApplication @LoadRedis public class StarterTestDemoApplication { public static void main(String[] args) { SpringApplication.run(StarterTestDemoApplication.class, args); }}Copy the code
  5. Create a RedisController class for testing

    Since it is only for testing purposes, the business logic is written directly in the Controller

    package com.example.startertestdemo.com.study; import com.study.redis.helper.RedisHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author markcwg * @date 2021/8/10 4:19 PM */ @restController @requestMapping ("redis") public class RedisController  { private RedisHelper redisHelper; @Autowired public RedisController(RedisHelper redisHelper) { this.redisHelper = redisHelper; } @PostMapping public String save(String key, String value) { redisHelper.set(key, value); return "success"; } @GetMapping public String get(String key) { return redisHelper.get(key); }}Copy the code
  6. Use postman tests to send data to Redis

  7. Get data from Redis using postman tests

5 End, scatter flowers!

Keep learning, keep loving!