两者有什么区别?
public UserBean() {
// create the session state
state = new BigInteger(64, new SecureRandom()).toString(32);
}和
public UserBean() {
init();
}
@PostConstruct
public void init() {
// create the session state
state = new BigInteger(64, new SecureRandom()).toString(32);
}其中状态是类的许多属性之一。
发布于 2018-02-22 14:20:28
您误解了@PostConstruct注释。
在执行依赖注入之后,容器应该调用带注释的方法。--您的应用程序代码不能调用。
所以当然使用没有容器的@PostConstruct (EJB,Spring,Guice.)没有任何意义。
@PostConstruct文档声明:
PostConstruct注释用于在执行任何初始化时执行依赖注入后需要执行的方法。
概括地说:
@PostConstruct方法注意,在步骤1、2和3之间,容器可能对其他bean执行其他任务,但是您不应该担心这一点,因为javadoc还声明在类投入服务之前必须调用@PostConstruct方法。
https://stackoverflow.com/questions/48929561
复制相似问题