Spring Redis – 从application.properties文件中读取配置
我有Spring Redis使用spring-data-redis
,所有默认配置都喜欢localhost
默认port
等等。
现在我尝试通过在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
我试过的,
- 我可以使用
@PropertySource
,然后注入@Value
并获取值。 但我不想那样做,因为这些属性不是由我定义的,而是来自Spring。 - 在本文档Spring Redis文档中 ,它仅表示可以使用属性进行配置,但不显示具体示例。
- 我还浏览了Spring Data Redis API类,发现
RedisProperties
应该对我有所帮助,但仍然无法弄清楚究竟如何告诉Spring从属性文件中读取。
您可以使用@PropertySource
从application.properties或您想要的其他属性文件中读取选项。 请查看PropertySource用法示例和使用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
目前( 2015年12月 ), application.properties
中的RedisSentinelConfiguration
选项对RedisSentinelConfiguration
支持有限:
请注意,目前只有Jedis和生菜生菜支持Redis Sentinel。
您可以在官方文档中阅读更多相关信息。
看得更深,我发现了这个,它可能是你想要的吗?
# 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.
参考: https: //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html Searchterm Redis
从我可以看到的值已经存在并被定义为
spring.redis.host=localhost # Redis server host. spring.redis.port=6379 # Redis server port.
如果你想创建自己的属性,你可以在这个post中查看我之前的post。
我在spring boot 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
这是一个解决您的问题的优雅解决方案:
@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 redisTemplate() { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(jedisConnectionFactory()); return redisTemplate; } @Bean RedisCacheManager cacheManager() { return new RedisCacheManager(redisTemplate()); } }
所以,恢复:
- 为@PropertySource添加特定名称
- 注入ConfigurableEnvironment而不是Environment
- 使用您在@PropertySource上提到的名称在ConfigurableEnvironment中获取PropertiesPropertySource
- 使用此PropertySource对象构造RedisSentinelConfiguration对象
- 不要忘记在属性文件中添加’spring.redis.sentinel.master’和’spring.redis.sentinel.nodes’属性
在我的工作区中测试,问候
我想这就是你要找的http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html
您可以使用ResourcePropertySource生成PropertySource对象。
PropertySource propertySource = new ResourcePropertySource("path/to/your/application.properties");
然后将其传递给RedisSentinelConfiguration的构造函数。
在每个测试类中使用@DirtiesContext(classMode = classmode.AFTER_CLASS)
。 这肯定会对你有用。
这对我有用:
@Configuration @EnableRedisRepositories public class RedisConfig { @Bean public JedisConnectionFactory jedisConnectionFactory() { RedisProperties properties = redisProperties(); RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); configuration.setHostName(properties.getHost()); configuration.setPort(properties.getPort()); return new JedisConnectionFactory(configuration); } @Bean public RedisTemplate redisTemplate() { final RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(jedisConnectionFactory()); template.setValueSerializer(new GenericToStringSerializer<>(Object.class)); return template; } @Bean @Primary public RedisProperties redisProperties() { return new RedisProperties(); } }
和属性文件:
spring.redis.host=localhost spring.redis.port=6379
@Autowired private JedisConnectionFactory connectionFactory; @Bean JedisConnectionFactory connectionFactory() { return connectionFactory }
- Spring REST,JSON“无法处理托管/后向引用’defaultReference’”415不支持的媒体类型
- Java – SpringMVC – 在控制器中获取参数
- 设置类路径以便在mysql和jdbc之间进行连接
- Apache POI货币数据格式
- 以自然顺序存储集合中的唯一元素
- Eclipse:错误启动静态资源java.lang.IllegalArgumentException
- 如何使用AffineTransform.quadrantRotate旋转位图?
- 为什么我们将firefoxdriver实例分配给webdriver
- Javareflection:在自定义AbstractProcessor中查找方法用法