Spring Redis - Read configuration from application.properties file

后端 未结 9 932
渐次进展
渐次进展 2020-12-25 13:46

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

Now

相关标签:
9条回答
  • 2020-12-25 14:11

    Use @DirtiesContext(classMode = classmode.AFTER_CLASS) at each test class. This will surely work for you.

    0 讨论(0)
  • 2020-12-25 14:14

    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.

    0 讨论(0)
  • 2020-12-25 14:16

    I think this what you are looking for http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html

    0 讨论(0)
  • 2020-12-25 14:18

    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.

    0 讨论(0)
  • 2020-12-25 14:21

    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

    0 讨论(0)
  • 2020-12-25 14:23

    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 :

    • add a specific name for the @PropertySource
    • inject a ConfigurableEnvironment instead of Environment
    • get the PropertiesPropertySource in your ConfigurableEnvironment using the name you mentioned on your @PropertySource
    • Use this PropertySource object to construct your RedisSentinelConfiguration object
    • Don't forget to add 'spring.redis.sentinel.master' and 'spring.redis.sentinel.nodes' properties in you property file

    Tested in my workspace, Regards

    0 讨论(0)
提交回复
热议问题