手写一个Spring Boot Starter

今天和大家分享Spring Boot的自动装配原理,并手写一个Starter加深对Spring Boot的理解。

众所周知,Spring Boot为了简化开发,省去了Spring中很多的XML文件。为了理解Spring Boot中自动装配的原理,今天我们基于这个机制自己动手实现一个Starter组件。

项目结构

手写一个Spring Boot Starter_第1张图片

步骤

  1. 新建一个名为:redis-spring-boot-starter的Maven项目
  2. 添加Maven依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.11.1</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.3.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  1. 定义属性,实现在applications.properties中的Redis的连接参数。
@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() {
        return host;
    }

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

    public String getPassword() {
        return password;
    }

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

    public int getPort() {
        return port;
    }

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

    public int getTimeout() {
        return timeout;
    }

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

    public boolean isSsl() {
        return ssl;
    }

    public void setSsl(boolean ssl) {
        this.ssl = ssl;
    }
}
  1. 定义需要自动装配的配置类
@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());
        }
        return Redisson.create(config);
    }
}
  1. resource文件夹下新建META-INF/spring.fatories文件,使Spring Boot程序可以扫描文件完成自动装配
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.jonssonyan.config.RedissonAutoConfiguration
  1. 打包成jar包
  2. 在其他项目引入当先依赖
        <dependency>
            <groupId>com.jonssonyan</groupId>
            <artifactId>redis-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  1. properties中配置host等RedisProperties中定义的属性

手写一个Spring Boot Starter_第2张图片

总结

通过一个简单的例子,让我们加深理解Spring Boot的自动装配原理,让我们对Spring Boot有个更深的理解。

你可能感兴趣的:(Java,spring,boot,java,redis)