手写一个简单的Starter
Posted 泡^泡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手写一个简单的Starter相关的知识,希望对你有一定的参考价值。
Starter的命令规范
Starter的命令规范有两种,一种是官方命令,另外一种是自定义命名。
- 官方命名格式:spring-boot-starter-模块名称,比如:spring-boot-starter-web
- 自定义命名格式:模块名称-spring-boot-starter, 比如:mybatis-spring-boot-starter
注解
- @ConditionalOnClass(Redisson.class):表示Redisson.class存在时才会继续进行下面的配置;
- @EnableConfigurationProperties(RedissonProperties.class):从IOC容器中获取application.properties配置文件的Bean;
- @Configuration表名此类是个配置类;
- @ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的。
- @Bean:把当前类加载到IOC容器里;
- -@Value:从Properties里取到值;
项目结构
代码实现
1.创建一个Maven工程
2.加载核心依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.1.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
</dependencies>
3.定义属性类
package com.redisson;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "redisson")
public class RedissonProperties
private String host="localhost";
private int port = 6379;
private String password;
private int timeout; //超时时间
private boolean ssl;
public String getPassword()
return password;
public void setPassword(String password)
this.password = password;
public String getHost()
return host;
public void setHost(String host)
this.host = host;
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;
4.自动配置类
package com.redisson;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedissonProperties.class)
@Configuration
public class RedissonAutoConfiguration
@Bean
RedissonClient redissonClient(RedissonProperties redissonProperties)
Config config = new Config();
String prefix = "redis://";
if(redissonProperties.isSsl())
prefix = "rediss://";
config.useSingleServer().setAddress(prefix+redissonProperties.getHost()+":"+redissonProperties.getPort()).setPassword(redissonProperties.getPassword()).setConnectTimeout(redissonProperties.getTimeout());
return Redisson.create(config);
5.创建spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\
com.redisson.RedissonAutoConfiguration
6.配置提示工具
additional-spring-configuration-metadata.json
"properties":[
"name":"redisson.host",
"type":"java.lang.String",
"description":"redis的服务器地址",
"defaultValue":"localhost"
,
"name":"redisson.port",
"type":"java.lang.Integer",
"description":"redis的服务器端口",
"defaultValue":6379
,
"name":"redisson.password",
"type":"java.lang.String",
"description":"redis的服务器密码"
]
7.打包 mvn install
8.创建一个Spring Boot项目测试
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
package com.springbootstarterdemo;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@SpringBootApplication
@RestController
public class SpringbootStarterDemoApplication
@Autowired
private RedissonClient redissonClient;
public static void main(String[] args)
SpringApplication.run(SpringbootStarterDemoApplication.class, args);
@GetMapping("/test/hello")
public String test()
setString("test","aaa");
getString("test");
System.out.println("查询出来的值:"+getString("test"));
return "666";
public String getString(String key)
RBucket<Object> result = this.redissonClient.getBucket(key);
return result.get().toString();
public void setString(String key, Object value)
RBucket<Object> result = this.redissonClient.getBucket(key);
if (!result.isExists())
result.set(value, 5, TimeUnit.MINUTES);
以上是关于手写一个简单的Starter的主要内容,如果未能解决你的问题,请参考以下文章
手写自定义springboot-starter,感受框架的魅力和原理
面试官:如何手写一个Spring Boot Starter?