發生場景以及探索
發生場景:在使用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