我让Spring Redis使用spring-data-redis所有默认配置(例如localhostdefault port等)。
spring-data-redis
localhost
port
现在,我试图通过在application.properties文件中进行配置来进行相同的配置。但是我无法弄清楚应该如何准确地读取属性值来创建bean。
application.properties
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
spring.redis.sentinel.master = themaster
spring.redis.sentinel.nodes = 192.168.188.231:26379
spring.redis.password = 12345
我尝试过的
@PropertySource
@Value
RedisProperties
您可以用来@PropertySource从application.properties或所需的其他属性文件中读取选项。请查看PropertySource用法示例和用法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<Object, Object> redisTemplate() { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>(); redisTemplate.setConnectionFactory(jedisConnectionFactory()); return redisTemplate; } @Bean RedisCacheManager cacheManager() { RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate()); return redisCacheManager; } }
当前( 2015年12月 )中的 spring.redis.sentinel 选项application.properties仅提供RedisSentinelConfiguration以下有限的支持:
RedisSentinelConfiguration
请注意,当前只有Jedis和生菜生菜支持Redis Sentinel。
您可以在官方文档中阅读有关此内容的更多信息。