Hi everyone, I am Zhuang, a back-end programmer, today I will share with you the principle of automatic assembly of Spring Boot, and handwritten a Starter to deepen the understanding of Spring Boot.

As we all know, Spring Boot eliminates many of the XML files in Spring to simplify development. In order to understand the principle of autoloader in Spring Boot, today we will implement a Starter component based on this mechanism.

The project structure

steps

  1. Create a new one called:redis-spring-boot-starterThe Maven project
  2. Adding Maven dependencies
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.111.</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.32..RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Copy the code
  1. Define properties that implement Redis connection parameters in applications. Properties.
@ConfigurationProperties(prefix = "gp.redisson")
public class RedissonProperties {
    private String host = "localhost";
    private String password;
    private int port = 6379;
    private int timeout;
    private boolean ssl;

    public String getHost(a) {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword(a) {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPort(a) {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getTimeout(a) {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public boolean isSsl(a) {
        return ssl;
    }

    public void setSsl(boolean ssl) {
        this.ssl = ssl; }}Copy the code
  1. Define the configuration classes that need to be auto-assembled
@Configuration
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {
    @Bean
    RedissonClient redissonClient(RedissonProperties redissonProperties) {
        Config config = new Config();
        String prefix = "redis://";
        if (redissonProperties.isSsl()) {
            prefix = "rediss://";
        }
        SingleServerConfig singleServerConfig = config.useSingleServer()
                .setAddress(prefix + redissonProperties.getHost() + ":" + redissonProperties.getPort())
                .setConnectTimeout(redissonProperties.getTimeout());
        if(! StringUtils.hasText(redissonProperties.getPassword())) { singleServerConfig.setPassword(redissonProperties.getPassword()); }returnRedisson.create(config); }}Copy the code
  1. inresourceNew folderMETA-INF/spring.fatoriesFile, so that Spring Boot programs can scan the file to complete automatic assembly
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jonssonyan.config.RedissonAutoConfiguration
Copy the code
  1. Package as jar packages
  2. Introduce leading dependencies in other projects
        <dependency>
            <groupId>com.jonssonyan</groupId>
            <artifactId>redis-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
Copy the code
  1. inpropertiesTo configure hostRedisPropertiesAttributes defined in

conclusion

Through a simple example, let’s deepen the understanding of Spring Boot automatic assembly principle, let’s have a deeper understanding of Spring Boot.

I am Zhuang, follow the wechat public number: Science and technology cat, get the first time update, I am under the period