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

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

问题描述

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

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 中,@PostConstruct 在常规 Java 对象构造函数之后调用.
当构造函数被调用时,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天全站免登陆