Spring的@PostConstruct注解

今天遇到一个问题,本来想在构造函数里面使用 @Autowired 注入的对象b,结果b为null。但是通过@PostConstruct 注解获取的对象可以。

@Component
public class AA{

	@Autowired
	private BB b;

	AA() {
		System.out.println("AA-----");
		this.iv = b.getKeystoreIv();
	}

	@PostConstruct
	public void init() {
		System.out.println("init----");
		this.iv = b.getKeystoreIv();
	}
	
	private String iv;
}

@PostConstruct注解,它的用途: 被注解的方法,在对象加载完依赖注入后执行。

构造函数执行的时候,依赖注入还没加载完成,导致b获取为null。


版权声明:本文为huqianlei原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。