一、简介
SpringBoot 最强大的功能就是把我们常用的场景抽取成了一个个starter(场景启动器),我们通过引入springboot 为我提供的这些场景启动器,我们再进行少量的配置就能使用相应的功能。即使是这样,springboot也不能囊括我们所有的使用场景,往往我们需要自定义starter,来简化我们对springboot的使用。
二、自定义starter
starter命名规则:官方名称:spring-boot-starter-xxx 第三方命名:xxx-spring-boot-starter
1. 创建项目
我们需要先创建一个项目命名:redis-spring-boot-starter
2. 引入相关依赖
自动配置相关的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
redis相关依赖:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
3. 编写redis配置类
在使用Spring官方的Starter时通常可以在application.properties中来配置参数覆盖掉默认的值,例如在使用redis时一般就会有对应的RedisProperties
@ConfigurationProperties(prefix="redis")
public class RedisProperties {
private String host = "127.0.0.1";
private Integer port = 6379;
private String password = "";
private Config config = new Config();
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Config getConfig() {
return config;
}
public void setConfig(Config config) {
this.config = config;
}
@Override
public String toString() {
return "RedisProperties [host=" + host + ", port=" + port + ", password=" + password + ", config=" + config
+ "]";
}
}
public class Config {
private Integer maxIdle = 50;
private Integer maxTotal = 100;
private Integer timeout = 5000;
private Integer maxWaitMillis = 3000;
private Boolean testOnBorrow = true;
public Integer getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(Integer maxIdle) {
this.maxIdle = maxIdle;
}
public Integer getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(Integer maxTotal) {
this.maxTotal = maxTotal;
}
public Integer getTimeout() {
return timeout;
}
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
public Integer getMaxWaitMillis() {
return maxWaitMillis;
}
public void setMaxWaitMillis(Integer maxWaitMillis) {
this.maxWaitMillis = maxWaitMillis;
}
public Boolean getTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(Boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
@Override
public String toString() {
return "Config [maxIdle=" + maxIdle + ", maxTotal=" + maxTotal + ", timeout=" + timeout + ", maxWaitMillis="
+ maxWaitMillis + ", testOnBorrow=" + testOnBorrow + "]";
}
}
4. 自动配置类
一般每个starter都至少会有一个自动配置类,一般命名规则使用XxxAutoConfiguration, 例如RedisAutoConfiguration
@SpringBootConfiguration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(Jedis.class)
public class RedisAutoConfiguration {
@Autowired
private RedisProperties redisProperties;
@Bean
public RedisConnectionFactory createRedisConnectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
connectionFactory.setHostName(redisProperties.getHost());
connectionFactory.setPort(redisProperties.getPort());
connectionFactory.setPassword(redisProperties.getPassword());
connectionFactory.setTimeout(redisProperties.getConfig().getTimeout());
connectionFactory.getPoolConfig().setMaxIdle(redisProperties.getConfig().getMaxIdle());
connectionFactory.getPoolConfig().setMaxTotal(redisProperties.getConfig().getMaxTotal());
connectionFactory.getPoolConfig().setMaxWaitMillis(redisProperties.getConfig().getMaxWaitMillis());
connectionFactory.getPoolConfig().setTestOnBorrow(redisProperties.getConfig().getTestOnBorrow());
return connectionFactory;
}
@Bean
public RedisTemplate taskRedisTemplate() {
RedisTemplate template = new StringRedisTemplate();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new JdkSerializationRedisSerializer());
template.setConnectionFactory(createRedisConnectionFactory());
return template;
}
}
5.spring.factories
在 resources 下创建文件夹 META-INF 并在 META-INF 下创建文件 spring.factories ,在该文件中配置自己的自动配置类。
目录结构:
spring.factories文件内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.reyco.redis.core.autoConfiguration.RedisAutoConfiguration
到这儿,我们的配置自定义的starter就写完了...
三. 测试自定义starter
1. 创建测试项目,引入自定义redis依赖
<!-- 自定义redis start -->
<dependency>
<groupId>com.reyco.redis</groupId>
<artifactId>redis-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2. 配置application.properties
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
redis.config.maxIdle=50
redis.config.maxTotal=100
redis.config.timeout=5000
redis.config.maxWaitMillis=5000
redis.config.testOnBorrow=true
3. 测试controller
@RestController
public class TestRedisController {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@RequestMapping(value = "/test/redis")
public String redis() {
ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
opsForValue.set("name", "admin");
String name = opsForValue.get("name");
System.out.println("name="+name);
return "name="+name;
}
}
效果:
starter引用成功。。。。。。