2015-12-10 158 views
9

我有Spring Redis使用spring-data-redis工作,所有默认配置都喜欢localhost默认port等等。Spring Redis - 从application.properties文件读取配置

现在我试图通过在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 =大师

spring.redis.sentinel.nodes = 192.168.188.231 :26379

spring.redis.password = 12345

我试了一下,

  1. 我都不可能用@PropertySource然后注入@Value和获取值。但我不想这样做,因为这些属性不是我定义的,而是来自Spring。
  2. 在本文件Spring Redis Documentation中,它只是表示可以使用属性进行配置,但不显示具体示例。
  3. 我也经历了Spring Data Redis API类,发现RedisProperties应该可以帮到我,但还是无法弄清楚究竟如何告诉Spring从属性文件中读取。
+0

目前使用做'@ Value'注解,什么更好的建议 –

+0

这是碰到的问题,通过写评论的正确方法: - ) –

回答

17

您可以使用@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<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; 
    } 
} 

在目前的时间(2015年12月)的spring.redis.sentinel在application.properties选项有限支持的RedisSentinelConfiguration

请注意,目前只有Jedis和生菜Lettuce支持Redis Sentinel。

你可以在official documentation了解更多。

+0

谢谢你回答,这是我的方法在我问这个问题之前使用。但是让我举一个例子,'spring-boot-starter-data-jpa',在这里如果你想配置数据源,你只需设置'spring.datasource.url'属性,spring会发挥它的魔力。并且因为它的标准应用程序属性之一(https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html)。我们不必为阅读而编写任何特定的逻辑,那么对于Redis来说,也应该有一些方法来实现这一点,也可以通过创建bean来实现。 –

+0

是的,我明白了。但是默认的application.properties不支持标记属性。所以你应该用我上面提到的方式。 – misterion

+1

'RedisSentinelConfiguration'有一个带有'PropertySource'的构造函数,这个构造函数已经有了读取这两个特定属性的逻辑。我现在想弄明白,我怎么能通过我的'application.properties'的'PropertySource'' –

1

我发现这个弹簧引导DOC部内24第7段

@Component 
@ConfigurationProperties(prefix="connection") 
public class ConnectionSettings { 

    private String username; 

    private InetAddress remoteAddress; 

    // ... getters and setters 

} 

属性可以随后经由connection.property

参考链路来修改:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties

+0

我会试试这个并更新你,如果这是我真正想要的。 –

+0

它工作还是需要更多帮助? –

+0

嗯,我没有尝试这种方法,因为方法标记为答案适合我,但感谢回答可能会帮助其他人 –

3

在寻找更深我发现这个,它可能是你在找什么?

# REDIS (RedisProperties) 
spring.redis.database=0 # Database index used by the connection factory. 
spring.redis.host=localhost # Redis server host. 
spring.redis.password= # Login password of the redis server. 
spring.redis.pool.max-active=8 # Max number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. 
spring.redis.pool.max-idle=8 # Max number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections. 
spring.redis.pool.max-wait=-1 # Maximum amount of time (in milliseconds) a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely. 
spring.redis.pool.min-idle=0 # Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive. 
spring.redis.port=6379 # Redis server port. 
spring.redis.sentinel.master= # Name of Redis server. 
spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs. 
spring.redis.timeout=0 # Connection timeout in milliseconds. 

Refernce:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html搜索关键词的Redis

从我所看到的值已经存在,被定义为

spring.redis.host=localhost # Redis server host. 
spring.redis.port=6379 # Redis server port. 

,如果你想创建自己的属性,你可以看看我以前张贴在这个线程中。

+1

嗯,这只适用于您的链接中提到的属性,我的问题是特定于相关属性到'哨兵'配置。 'sentinel'也是默认的弹簧属性,但框架仍然不读取它们,连接也没有建立。 –

0

可以使用ResourcePropertySource生成一个PropertySource对象。

PropertySource propertySource = new ResourcePropertySource("path/to/your/application.properties"); 

然后将它传递给RedisSentinelConfiguration的构造函数。

0
@Autowired 
private JedisConnectionFactory connectionFactory; 

@Bean 
JedisConnectionFactory connectionFactory() { 
    return connectionFactory 
} 
+0

这不是一个答案 – sebadagostino

0

这里是一个完美的解决方案,以解决您的问题:

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

} 

因此,恢复:

  • 为@PropertySource
  • 添加特定的名字注入ConfigurableEnvironment代替环境
  • 在您的ConfigurableEnvironment中获取PropertiesPropertySource us荷兰国际集团您在@PropertySource
  • 使用该PropertySource对象中提到的名称来构建你的RedisSentinelConfiguration对象
  • 不要忘记加上“spring.redis.sentinel.master”和“spring.redis.sentinel.nodes”属性在你的属性文件

经测试,在我的工作区, 问候