发生场景以及探索
发生场景:在使用jedis的连接池管理redis时,需要手动写入连接池相关配置。
yml中的配置
#配置缓存
spring:
#redis:
# port: 6379
# host: 106.13.46.205
# timeout: 3
# jedis:
# pool:
# max-active: 8
# max-idle: 10
# max-wait: 3
本想手动写入到JavaBean对象中
@Configuration
@Component
@Data
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.post}")
private Integer post;
@Value("${spring.redis.timeout}")
private Integer timeout;
@Value("${spring.redis.jedis.pool.max-active}")
private Integer maxActive;
@Value("${spring.redis.jedis.pool.max-idle}")
private Integer maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private Integer maxWait;
}
后来发现存在类型不匹配问题,尝试多种办法都发现实用性不高。最后恍然大悟,yml文件就是properties的变形,它不同于xml文件有约束,我可以自定义自己想定义的东西,于是乎。
解决办法
redis:
host: 106.13.46.205
port: 6379
timeout: 3
maxActive: 8
maxIdle: 10
maxWait: 3
自定义了一个redis开头文件,并且相关属性
@ConfigurationProperties(prefix = "redis")
@Component
@Data
public class RedisConfig {
private String host;
private Integer port;
private Integer timeout;
private Integer maxActive;
private Integer maxIdle;
private Integer maxWait;
}
如此,所有问题迎刃而解,简单实用,并且不需要挨个的@Value