我有Spring Redis使用spring-data-redis工作,所有默认配置都喜欢localhost默认端口,依此类推.
现在我尝试通过在application.properties文件中配置它来进行相同的配置.但我无法弄清楚我应该如何创建完全符合我的属性值的bean.
Redis配置文件
@EnableRedisHttpSession
@Configuration
public class SpringSessionRedisConfiguration {
@Bean
JedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory();
}
@Autowired
@Bean
RedisCacheManager redisCacheManager(final StringRedisTemplate stringRedisTemplate) {
return new RedisCacheManager(stringRedisTemplate);
}
@Autowired
@Bean
StringRedisTemplate template(final RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
}
application.properties中的标准参数
spring.redis.sentinel.master=themaster
spring.redis.sentinel.nodes=192.168.188.231:26379
spring.redis.password=12345
我试过的,
>我可以使用@PropertySource然后注入@Value并获取值.但是我不想这样做,因为这些属性不是由我定义的,而是来自Spring.
>在本文档Spring Redis Documentation中,它仅表示可以使用属性进行配置,但不显示具体示例.
>我还经历了Spring Data Redis API类,发现RedisProperties应该对我有所帮助,但仍然无法弄清楚如何告诉Spring从属性文件中读取.
解决方法:
您可以使用@PropertySource从application.properties或您想要的其他属性文件中读取选项.请看PropertySource usage example并工作example of usage spring-redis-cache.或者看看这个小样本:
@Configuration
@PropertySource("application.properties")
public class SpringSessionRedisConfiguration {
@Value("${redis.hostname}")
private String redisHostName;
@Value("${redis.port}")
private int redisPort;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(redisHostName);
factory.setPort(redisPort);
factory.setUsePool(true);
return factory;
}
@Bean
RedisTemplate redisTemplate() {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}
@Bean
RedisCacheManager cacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
return redisCacheManager;
}
}
目前(2015年12月),application.properties中的spring.redis.sentinel选项对RedisSentinelConfiguration的支持有限:
Please note that currently only 07002 and lettuce 07003 support Redis Sentinel.
标签:java,spring,redis,configuration
来源: https://codeday.me/bug/20191004/1853366.html