JSF-@PostConstruct和从构造方法直接调用之间有什么区别? [英] JSF - what is the difference between @PostConstruct and direct method call from constructor?

查看:577
本文介绍了JSF-@PostConstruct和从构造方法直接调用之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的范围为ManagedBean的视图中,我需要用数据库中的数据填充一个列表. 我是通过构造函数的直接调用来做到这一点的,就像这样:

In my view scoped managedBean, i need to populate a list with data from DB. Im doing this through a direct call from the constructor, something like this:

public MyClass(){
   list=populateFromDb();
}

但是可以在带有@PostConstruct注释的方法中调用此方法,例如:

but this method can be called in a @PostConstruct annotated method, like:

public MyClass(){
}

@PostConstruct
populateFromDb(){...}

这有什么区别?

推荐答案

如果bean具有请求范围,则@PostConstruct每次都会执行.在实例化托管bean之后但在将bean放入作用域之前,将调用该方法.这样的方法不带参数,返回void,并且可能不声明要抛出的检查异常.方法可以是公共的,受保护的,私有的或包私有的.如果该方法引发未经检查的异常,则JSF实现一定不能将托管Bean投入使用,并且该托管Bean实例上的其他方法都不会被调用.

If the bean has request scope, @PostConstruct will get executed every time. It will be called after the managed bean is instantiated, but before the bean is placed in scope. Such a method take no arguments, return void, and may not declare a checked exception to be thrown. Method may be public, protected, private, or package private. If the method throws an unchecked exception, the JSF implementation must not put the managed bean into service and no further menthods on that managed bean instance will be called.

public TrainingClassForm() {

    }
  @PostConstruct
   public void init() {
       if (this.trainingListModel.getListDataModel() != null) {
          this.trainingListModel.getAllTrainingClasses();
       }

    }


让您重温这个堆栈问题
在托管bean中,<在常规Java对象构造函数之后调用c0>.
在调用构造函数时,bean尚未初始化-即未注入任何依赖项. @PostConstruct方法中,bean已完全初始化,您可以使用依赖项

@PostConstruct是确保在bean生命周期中仅调用此方法一次的合同.可能会发生(尽管不太可能)容器在其内部工作中多次实例化bean,但是它保证@PostConstruct仅被调用一次.
如果您的类在构造函数中执行了所有初始化操作,则@PostConstruct确实是多余的.
但是,如果您的类使用setter方法注入了其依赖项,则该类的构造函数无法完全初始化该对象,有时在调用所有setter方法之后需要执行一些初始化,因此@PostConstruct
请参见


Reffering you to this question of stack
In a managed bean, @PostConstruct is called after the regular Java object constructor.
when the constructor is called, the bean is not yet initialized - i.e. no dependencies are injected. In the @PostConstruct method the bean is fully initialized and you can use the dependencies

@PostConstruct is the contract that guarantees that this method will be invoked only once in the bean lifecycle . It may happen (though unlikely) that a bean is instantiated multiple times by the container in its internal working, but it guarantees that @PostConstruct will be invoked only once.
If your class performs all of its initialization in the constructor, then @PostConstruct is indeed redundant.
However, if your class has its dependencies injected using setter methods, then the class's constructor cannot fully initialize the object, and sometimes some initialization needs to be performed after all the setter methods have been called, hence the use case of @PostConstruct
Also see this and this

这篇关于JSF-@PostConstruct和从构造方法直接调用之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆