<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <!-- <version>1.3.5.RELEASE</version> --> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <!-- <version>1.3.6.RELEASE</version> --> </dependency>
@Component
public class MyBean {
private StringRedisTemplate template;
@Autowired
public MyBean(StringRedisTemplate template) {
this.template = template;
}
// ...
}
@Component
public class MyBean {
private StringRedisTemplate template;
@Autowired
public MyBean(StringRedisTemplate template) {
this.template = template;
}
// ...
}
@Configuration
public class Redis137_11Configuration {
@Bean(name = "redis123Template")
public StringRedisTemplate redisTemplate(
@Value("${redis123.hostName}") String hostName,
@Value("${redis123.port}") int port,
@Value("${redis123.password}") String password,
@Value("${redis123.maxIdle}") int maxIdle,
@Value("${redis123.maxTotal}") int maxTotal,
@Value("${redis123.index}") int index,
@Value("${redis123.maxWaitMillis}") long maxWaitMillis,
@Value("${redis123.testOnBorrow}") boolean testOnBorrow) {
StringRedisTemplate temple = new StringRedisTemplate();
temple.setConnectionFactory(connectionFactory(hostName, port, password,
maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));
return temple;
}
public RedisConnectionFactory connectionFactory(String hostName, int port,
String password, int maxIdle, int maxTotal, int index,
long maxWaitMillis, boolean testOnBorrow) {
JedisConnectionFactory jedis = new JedisConnectionFactory();
jedis.setHostName(hostName);
jedis.setPort(port);
if (!StringUtils.isEmpty(password)) {
jedis.setPassword(password);
}
if (index != 0) {
jedis.setDatabase(index);
}
jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis,
testOnBorrow));
// 初始化连接pool
jedis.afterPropertiesSet();
RedisConnectionFactory factory = jedis;
return factory;
}
public JedisPoolConfig poolCofig(int maxIdle, int maxTotal,
long maxWaitMillis, boolean testOnBorrow) {
JedisPoolConfig poolCofig = new JedisPoolConfig();
poolCofig.setMaxIdle(maxIdle);
poolCofig.setMaxTotal(maxTotal);
poolCofig.setMaxWaitMillis(maxWaitMillis);
poolCofig.setTestOnBorrow(testOnBorrow);
return poolCofig;
}
}
public abstract class AbRedisConfiguration {
protected StringRedisTemplate temple;
public void setData(String key, String value) {
getTemple().opsForValue().set(key, value);
}
public String getData(String key) {
return getTemple().opsForValue().get(key);
}
public StringRedisTemplate getTemple() {
return temple;
}
}
public class Redis123 extends AbRedisConfiguration {
@Resource(name = "redis123Template")
private StringRedisTemplate temple;
public StringRedisTemplate getTemple() {
return temple;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有