项目结构
我这里模拟的是一个spring整合lettuce来操作redis的start
1. xxx-spring-boot-start: 空项目,pom中引入了autoconfiguraton项目
2. xxx-spring-boot-autoconfiguraton:具体逻辑,暴露功能
2.xxx-spring-boot-start
pom.xml
<dependencies>
<dependency>
<groupId>com.hui</groupId>
<artifactId>my-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
3.xxx-spring-boot-autoconfiguraton
pom.xml
<groupId>com.hui</groupId>
<artifactId>my-spring-boot-autoconfigure</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<!--将被@ConfigurationProperties注解的类的属性注入到元数据-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<version>2.3.3.RELEASE</version>
<optional>true</optional>
</dependency>
<!-- spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<!-- lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.3.4.RELEASE</version>
</dependency>
</dependencies>
@Configuration
@ConditionalOnClass(RedisClient.class)
public class RedisConfiguration {
@Bean
public LettuceConnectionFactory lettuceConnectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration) {
return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration(MyRedisProperties myRedisProperties,RedisPassword redisPassword){
RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
standaloneConfiguration.setHostName(myRedisProperties.getHost());
standaloneConfiguration.setPort(myRedisProperties.getPort());
standaloneConfiguration.setPassword(redisPassword);
return standaloneConfiguration;
}
@Bean
public RedisPassword redisPassword(MyRedisProperties myRedisProperties) {
return RedisPassword.of(myRedisProperties.getPassword());
}
}
@Component
@ConfigurationProperties(prefix = "spring.lh.redis")
public class MyRedisProperties {
private String host;
private String password;
private int port;
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;
}
}
@Import(value = {RedisConfiguration.class, MyRedisProperties.class})
@ConditionalOnClass(RedisOperations.class)
public class MyRedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public RedisTemplate<String, String> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}
}
在resources中建立一个META-INF/spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hui.MyRedisAutoConfiguration
在测试项目中进行测试
1.application.properties:
spring.lh.redis.host=127.0.0.1
spring.lh.redis.port=6379
spring.lh.redis.password=admin
2.RedisService
public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
public void setKey(String key,String value){
redisTemplate.opsForValue().set(key,value);
}
}
3.springboot启动类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
RedisService redisService = (RedisService)context.getBean("redisService");
redisService.setKey("mykey","hello,Lance");
}
}
启动执行后查看redis