[Springboot] Hand-written Spring-boot-starter component

preface

Starter will include all the dependency packages used, avoiding the developer’s own hassle of importing dependencies.

Although different starter implementations vary, they generally use the same two things: ConfigurationProperties and AutoConfiguration.

Starter is a collection of dependency descriptors that you can easily add to your application. Or you can understand it, when we develop at ordinary times, in many cases there will be a module depends on another module, this time we are generally use maven module dependencies, dependence of modules, but in this case we can adopt the way of the Starter, will need to be dependent module with a Starter way to develop, Finally, introducing a Starter can do the same.

Naming rules

Because SpringBoot official itself provides a lot of Starter, in order to distinguish between those who are official and those who are third-party, so SpringBoot official proposed:

The Starter provided by the third party is xxX-spring-boot-starter

The official Starter is spring-boot-starter- XXX.

demand

We’ll use Redisson as an example to implement a simple starter component that relies on the Jars and beans required by the RedissonClient to our current project.

The project structure

To create a Starter

First we create a redisson-spring-boot-starter project and add the Redisson and spring-boot-starter dependencies as follows

<?xml version="1.0" encoding="UTF-8"? >

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.autu.example.redisson</groupId>
  <artifactId>redisson-spring-boot-starter</artifactId>
  <version>1.0 the SNAPSHOT</version>

  <name>redisson-spring-boot-starter</name>
  <! -- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
      <version>2.3.1. RELEASE</version>
      <! -- Disable passing dependencies -->
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.redisson</groupId>
      <artifactId>redisson</artifactId>
      <version>3.13.1</version>
    </dependency>
      <! Add this dependency -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <version>2.3.1. RELEASE</version>
    </dependency>
  </dependencies>
</project>
Copy the code

Writing configuration classes

Create a ConfigurationProperties to hold the configuration information

@ConfigurationProperties(prefix = "auto.redisson")
public class RedissonProperties {

    /** Redis server host */
    private String host = "localhost";

    /** Redis server port */
    private int port = 6379;

    /** Connection timeout time */
    private int timeout;

    /** Whether to enable SSL support */
    private booleanssl; . }Copy the code

Create automated configuration classes

  • To create aAutoConfigurationTo reference the defined configuration information
  • inAutoConfigurationTo achieve bean injection and read configuration information
  • Declare this class in the Spring. factories configuration file
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedissonProperties.class)
@Configuration
public class RedissonAutoConfiguration {

    @Bean
    RedissonClient redissonClient(RedissonProperties redissonProperties) {
        Config config = new Config();
		// Check whether SSL is enabled
        String prefix = redissonProperties.isSsl() ? "rediss://" : "redis://";

        String host = redissonProperties.getHost();
        int port = redissonProperties.getPort();

        config.useSingleServer()
                .setAddress(prefix + host + ":" + port)
                .setConnectTimeout(1000 * 30);

        returnRedisson.create(config); }}Copy the code

createspring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.autu.example.redisson.RedissonAutoConfiguration
Copy the code

createadditional-spring-configuration-metadata.json

Additional -spring-configuration-metadata.json provides configuration information that prompts other projects to rely on the current starter.

<! The starter configuration file prompts you to add this dependency to the starter
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <version>2.3.1. RELEASE</version>
</dependency>
Copy the code
{
  "properties": [{"name": "autu.redissin.host"."type": "java.lang.String"."description": "Redis server address."."defaultValue": "localhost"}, {"name": "autu.redisson.port"."type": "java.lang.Integer"."description": "Redis server port."."defaultValue": 6379}}]Copy the code

Next, we deploy the project as a jar package to the local repository for another service call by running the MVN install command.

test

Create a test project that relies on redisson-spring-boot-starter.

application.propertiesThe configuration file

Auto. Redisson. Host = 127.0.0.1 auto. Redisson. Port = 6379 auto redisson. Timeout = 10000 auto redisson. SSL = falseCopy the code

The test class

@RestController
public class HelloController {

    @Autowired
    RedissonClient redissonClient;

    @GetMapping("/test")
    public String say(a) {
        RBucket<Object> bucket = redissonClient.getBucket("name");
        if (bucket.get() == null) {
            bucket.set("bucket");
        }
        returnbucket.get().toString(); }}Copy the code

The test results

From the results, we see that the RedissonClient defined in starter has been successfully injected into the test project.

The original address

Autumn200.com/2020/07/03/…