привет, я хотел бы знать, как Springboot обрабатывает файл свойств.
я не пробовал
@ конфигурации
открытый класс RedisConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class);
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.pool.max-total:100}")
private int poolMaxTotal;
@Value("${spring.redis.pool.max-idle:100}")
private int poolMaxIdle;
@Value("${spring.redis.pool.min-idle:10}")
private int poolMinIdle;
@Value("${spring.redis.pool.max-wait:2000}")
private int poolMaxWaitMillis;
public @Bean RedisTemplate redisTemplateForUser() {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory());
redisTemplate.setKeySerializer(stringRedisSerializer());
redisTemplate.setValueSerializer(redisJsonSerializer());
return redisTemplate;
}
public @Bean StringRedisSerializer stringRedisSerializer(){
return new StringRedisSerializer();
}
public @Bean Jackson2JsonRedisSerializer redisJsonSerializer(){
return new Jackson2JsonRedisSerializer<>(UserFromCache.class);
}
public @Bean RedisTemplate redisTemplate() {
RedisTemplate redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory());
return redisTemplate;
}
public @Bean RedisConnectionFactory connectionFactory() {
JedisPoolConfig pool = new JedisPoolConfig();
pool.setMaxTotal(poolMaxTotal);
pool.setMaxIdle(poolMaxIdle);
pool.setMinIdle(poolMinIdle);
pool.setMaxWaitMillis(poolMaxWaitMillis);
pool.setTestOnBorrow(true);
JedisConnectionFactory jcf = new JedisConnectionFactory(pool);
jcf.setHostName(host);
обычно мы используем значение из файла свойств для настройки пула следующим образом. Я предполагаю, что Spring мог прочитать значение из свойств по этой аннотации (я не проверял, что делает эта аннотация). почему мы весной предоставляем класс, что за история:
#
package org.springframework.boot.autoconfigure.data.redis;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(
prefix = "spring.redis"
)
public class RedisProperties {
private int database = 0;
private String url;
private String host = "localhost";
private String password;
private int port = 6379;