Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Redis - Read configuration from application.properties file

I have Spring Redis working using spring-data-redis with all default configuration likes localhost default port and so on.

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 Configuration File

@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); } } 

Standard Parameters in application.properties

spring.redis.sentinel.master=themaster

spring.redis.sentinel.nodes=192.168.188.231:26379

spring.redis.password=12345

What I tried,

  1. 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.
  2. In this documentation Spring Redis Documentation, it only says that it can be configured using properties but doesn't show concrete example.
  3. 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.
like image 310
Shrikant Havale Avatar asked Dec 10 '15 11:12

Shrikant Havale


People also ask

How do I read application properties in spring boot?

Read Properties using the @ConfigurationProperties Annotation. Another way to read application properties in the Spring Boot application is to use the @ConfigurationProperties annotation. To do that, we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.

How do you specify file path in application properties in spring boot?

properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.

What is RedisConnectionFactory?

Provides a suitable connection for interacting with Redis. Returns: connection for interacting with Redis. Throws: IllegalStateException - if the connection factory requires initialization and the factory was not yet initialized.


1 Answers

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;     } } 

In present time (december 2015) the spring.redis.sentinel options in application.properties has limited support of RedisSentinelConfiguration:

Please note that currently only Jedis and lettuce Lettuce support Redis Sentinel.

You may read more about this in official documentation.

like image 65
Nick Bondarenko Avatar answered Sep 24 '22 14:09

Nick Bondarenko