@PostConstruct注解小结

时间:2023-03-09 03:40:55
@PostConstruct注解小结

1.在具体Bean的实例化过程中,@PostConstruct注解的方法,会在构造方法之后,init方法之前进行调用
2.在项目中@PostConstruct主要应用场景是在初始化Servlet时加载一些缓存数据等

举个例子,使用@PostConstruct注解:

Class ServiceA{
private Map<String, String> initMap; //在这里加载一些缓存数据
@PostConstruct
public void init() {
initMap = new HashMap<>(3);
initMap.put("A","the a");
initMap.put("B","the b");
initMap.put("C","the c");
} }