JSF属性初始化

时间:2015-03-05 15:29:38

标签: java jsf initialization

我遇到了以下问题: 我有以下JSF bean:

public class MailingBean {
    private RecipientService<?> recipientService;
    private SearchRecipientController searchRecipientController = new SearchRecipientController(recipientService);

    //Stuff, GET, SET
}

这个bean在faces-config.xml中初始化如下:

<managed-bean>
    <managed-bean-name>mailingBean</managed-bean-name>
    <managed-bean-class>path.to.package.MailingBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>recipientService</property-name>
        <value>#{playerService}</value>
    </managed-property>
</managed-bean>

new SearchRecipientController(recipientService)正在调用时,recipientService为空。是否可以以某种方式指定初始化顺序?或者我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您不能在构造函数中使用托管属性(或者在声明中使用托管属性)。你应该使用postconstruct方法来做你想做的事情。在构造对象之后设置manage属性。

private SearchRecipientController searchRecipientController;

@PostConstruct
public void init(){
     searchRecipientController = new SearchRecipientController(getRecipientService());
}

另一种方法是仅通过getter使用searchRecipientController。

public SearchRecipientController getSearchRecipientController(){
    if(searchRecipientController==null){
        searchRecipientController = new SearchRecipientController(getRecipientService());
    }
    return searchRecipientController;
}