问题
- 有一个 zk 配置,通过 @ConfigurationProperties 注释由 spring 容器自动初始化
@ConfigurationProperties(prefix = "jingwei.zookeeper")
public class ZkConfig {
private String hosts;
private String namespace;
private int sessionTimeout;
private int connectionTimeout;
}
- 但是我们要使用 ZkConfig 的地方是一个普通类 ZkManager,它没有 @controller 、@service、@repository 注解,也就不会被 spring 容器自动初始化。
- 如果在 ZkManager 中使用下面代码,然后 new ZkManager(),可以发现 zkConfig 为 null。
@Resource
private ZkConfig zkConfig;
方案1
- 在 ZkManager 类上加 @Component 注解。
- 使用 Resource 注解
@Resource
private ZkConfig zkConfig;
@PostConstruct
public void init(