java redis读取文件夹_java – Spring Redis – 从application.properties文件中读取配置...

这篇博客介绍了如何在Spring应用中使用Redis,并从`application.properties`文件中读取配置,包括设置JedisConnectionFactory和RedisCacheManager。通过@PropertySource和@Value注解,可以从属性文件中获取redis主机名、端口和密码等配置。
摘要由CSDN通过智能技术生成

我有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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值