gpt4 book ai didi

java - JSF 属性初始化

转载 作者:行者123 更新时间:2023-12-01 11:49:52 25 4
gpt4 key购买 nike

我遇到了以下问题:我有以下 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一片空白。是否可以以某种方式指定初始化顺序?或者我该如何解决这个问题?

最佳答案

您不能在构造函数中使用托管属性(或如您在声明中的情况)。您应该使用 postconstruct 方法来完成您想要做的事情。管理属性在对象构造后设置。

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;
}

关于java - JSF 属性初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28881575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com