Spring Redis - 从 application.properties 文件读取配置 [英] Spring Redis - Read configuration from application.properties file
问题描述
我让 Spring Redis 使用 spring-data-redis
和所有默认配置工作,比如 localhost
默认 port
等等.
I have Spring Redis working using spring-data-redis
with all default configuration likes localhost
default port
and so on.
现在我试图通过在 application.properties
文件中进行配置来进行相同的配置.但是我不知道我应该如何准确创建读取我的属性值的 bean.
Now I am trying to make the same configuration by configuring it in application.properties
file. But I cannot figure out how should I create beans exactly that my property values are read.
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 中的标准参数
Standard Parameters in application.properties
spring.redis.sentinel.master=themaster
spring.redis.sentinel.master=themaster
spring.redis.sentinel.nodes=192.168.188.231:26379
spring.redis.sentinel.nodes=192.168.188.231:26379
spring.redis.password=12345
spring.redis.password=12345
我试过的,
- 我可以使用
@PropertySource
然后注入@Value
并获取值.但我不想这样做,因为这些属性不是我定义的,而是来自 Spring. - 在本文档中 Spring Redis 文档,它只说可以使用属性进行配置,但没有显示具体示例.
- 我还浏览了 Spring Data Redis API 类,发现
RedisProperties
应该对我有帮助,但仍然无法弄清楚如何告诉 Spring 从属性文件中读取数据.
- I can possibly use
@PropertySource
and then inject@Value
and get the values. But I don't want to do that as those properties are not defined by me but are from Spring. - In this documentation Spring Redis Documentation, it only says that it can be configured using properties but doesn't show concrete example.
- I also went through Spring Data Redis API classes, and found that
RedisProperties
should help me, but still cannot figure out how exactly to tell Spring to read from properties file.
推荐答案
您可以使用 @PropertySource
从 application.properties 或您想要的其他属性文件中读取选项.请查看 PropertySource 使用示例 并使用 使用示例 spring-redis-cache.或者看看这个小样本:
You can use @PropertySource
to read options from application.properties or other property file you want. Please look PropertySource usage example and working example of usage spring-redis-cache. Or look at this small sample:
@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 月)application.properties
中的 spring.redis.sentinel 选项对 RedisSentinelConfiguration 的支持有限代码>:
In present time (december 2015) the spring.redis.sentinel options in application.properties
has limited support of RedisSentinelConfiguration
:
请注意,目前只有 Jedis 和生菜 Lettuce 支持Redis Sentinel.
Please note that currently only Jedis and lettuce Lettuce support Redis Sentinel.
您可以在 中阅读更多相关信息官方文档.
这篇关于Spring Redis - 从 application.properties 文件读取配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!