I have Spring Redis working using spring-data-redis
with all default configuration likes localhost
default port
and so on.
Now
Use @DirtiesContext(classMode = classmode.AFTER_CLASS)
at each test class. This will surely work for you.
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.
I think this what you are looking for http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html
You can use the ResourcePropertySource to generate a PropertySource object.
PropertySource propertySource = new ResourcePropertySource("path/to/your/application.properties");
Then pass it to the constructor of the RedisSentinelConfiguration.
I found this within the spring boot doc section 24 paragraph 7
@Component
@ConfigurationProperties(prefix="connection")
public class ConnectionSettings {
private String username;
private InetAddress remoteAddress;
// ... getters and setters
}
Properties can then be modified via connection.property
Reference link: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties
Here is an elegant solution to solve your issue :
@Configuration
@PropertySource(name="application", value="classpath:application.properties")
public class SpringSessionRedisConfiguration {
@Resource
ConfigurableEnvironment environment;
@Bean
public PropertiesPropertySource propertySource() {
return (PropertiesPropertySource) environment.getPropertySources().get("application");
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
return new JedisConnectionFactory(sentinelConfiguration(), poolConfiguration());
}
@Bean
public RedisSentinelConfiguration sentinelConfiguration() {
return new RedisSentinelConfiguration(propertySource());
}
@Bean
public JedisPoolConfig poolConfiguration() {
JedisPoolConfiguration config = new JedisPoolConfiguration();
// add your customized configuration if needed
return config;
}
@Bean
RedisTemplate<Object, Object> redisTemplate() {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
return redisTemplate;
}
@Bean
RedisCacheManager cacheManager() {
return new RedisCacheManager(redisTemplate());
}
}
so, to resume :
Tested in my workspace, Regards