一、簡介
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引用成功。。。。。。