@[TOC]
1 创建redis-spring-boot-autoconfigure模块
1.1 新建RedisProperties配置类
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {
private String host = "localhost";
private int port = 6379;
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;
}
}
1.2 新建RedisAutoConfiguration,注入bean
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.Jedis;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(Jedis.class)
public class RedisAutoConfiguration {
/**
* 提供Jedis jedis
* @return
*/
@Bean
@ConditionalOnMissingBean(name = "jedis")
public Jedis jedis(RedisProperties redisProperties){
System.out.println("RedisAutoConfiguration.......");
return new Jedis(redisProperties.getHost(),redisProperties.getPort());
}
}
1.3 新建spring.factories
- 路径为:resources\META-INF\spring.factories
内容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yunfeng.redis.config.RedisAutoConfiguration
1.4 pom的依赖如下
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 引入jedis依赖 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies>
2 创建redis-spring-boot-starter模块,并依赖redis-spring-boot-autoconfigure的模块
- 此模块只是包装作用,无需其他代码
pom依赖如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 引入配置模块 --> <dependency> <groupId>com.yunfeng</groupId> <artifactId>redis-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
3 新建测试模块,并引入自定义的redis-starter依赖,测试获取Jedis的Bean,并操作redis
3.1 测试模块的pom中引入自己定义的starter
<!-- 引入自定义redis的starter --> <dependency> <groupId>com.example</groupId> <artifactId>redis-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
3.2 测试模块的启动类里测试
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import redis.clients.jedis.Jedis;
@SpringBootApplication
public class SpringbootConditionApplication {
public static void main(String[] args) {
//启动Springboot的应用,返回Spring的IOC容器
ConfigurableApplicationContext context = SpringApplication.run(SpringbootConditionApplication.class, args);
Jedis jedis = context.getBean(Jedis.class);
System.out.println(jedis);
jedis.set("name","husiyuan");
String name = jedis.get("name");
System.out.println("name = " + name);
}
//若存在则不使用自定义starter中的Jedis
@Bean
public Jedis jedis(){
return new Jedis("localhost",6379);
}
}
```