1.引入需要的jar
这里我做的是Redison的starter
<dependencies>
<!--boot启动类-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.0.RELEASE</version>
<optional>true</optional>
</dependency>
<!--做一个redisson的starter-->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.11.5</version>
</dependency>
<!--解析自动提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
</dependencies>
2.写入对应配置类
自动装配的配置 和配置类属性
package com.dite.starter;
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;
/**
* Create by dgx 2022/4/3 14:24
*/
@ConditionalOnClass(Redisson.class)
@Configuration
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfituration {
@Bean
RedissonClient redissonClient(RedissonProperties redissonProperties){
Config config =new Config();
String prefix ="redis://";
if (redissonProperties.getUserSSL()){
prefix="rediss://";
}
config.useSingleServer().setAddress(prefix+redissonProperties.getHost()+":"+redissonProperties.getPort())
.setTimeout(redissonProperties.getTimeOutMillSecond());
return Redisson.create(config);
}
}
package com.dite.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Create by dgx 2022/4/3 14:26
*/
@ConfigurationProperties(prefix = "com.redisson")
public class RedissonProperties {
private String host;
private Integer port;
private Integer timeOutMillSecond;
private Boolean userSSL;
public Boolean getUserSSL() {
return userSSL;
}
public void setUserSSL(Boolean userSSL) {
this.userSSL = userSSL;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public Integer getTimeOutMillSecond() {
return timeOutMillSecond;
}
public void setTimeOutMillSecond(Integer timeOutMillSecond) {
this.timeOutMillSecond = timeOutMillSecond;
}
}
写入Spring自动装配的配置文件
- 在META-INF下创建spring.factories文件
key 写spring的字段装配 value 写配置类的全路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.dite.starter.RedissonAutoConfituration
- 创建additional-spring-configuration-metadata.json 这样在yaml文件能够自动提示
{
"properties": [{
"name": "com.redisson.host",
"description": "redis的ip地址.",
"defaultValue": "localhost",
"type":"java.lang.String"
},{
"name": "com.redisson.port",
"description": "redis的端口号.",
"defaultValue": 6379,
"type":"java.lang.Integer"
},
{
"name": "com.redisson.timeOutMillSecond",
"description": "连接超时时间单位毫秒.",
"defaultValue": 3000,
"type":"java.lang.Integer"
},
{
"name": "com.redisson.userSSL",
"description": "是否使用SSL连接.",
"defaultValue": false,
"type":"java.lang.Boolean"
}],
"groups": [{
"name": "com.redisson",
"type": "com.dite.starter.RedissonProperties",
"sourceType": "com.dite.starter.RedissonProperties"
}]
}