Spring Redis - 从 application.properties 文件中读取配置
2022-09-01 15:09:36
我有Spring Redis使用所有默认配置,如默认等。spring-data-redis
localhost
port
现在,我正在尝试通过在文件中配置它来进行相同的配置。但是我无法弄清楚我应该如何创建我的属性值被读取的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);
}
}
应用程序中的标准参数.属性
spring.redis.sentinel.master=themaster
spring.redis.sentinel.nodes=192.168.188.231:26379
spring.redis.password=12345
我尝试过,
- 我可以使用然后注入并获取值。但我不想这样做,因为这些属性不是由我定义的,而是来自Spring。
@PropertySource
@Value
- 在本文档Spring Redis文档中,它只说它可以使用属性进行配置,但没有显示具体示例。
- 我还浏览了Spring Data Redis API类,发现这应该对我有所帮助,但仍然无法弄清楚如何确切地告诉Spring从属性文件中读取。
RedisProperties