gpt4 book ai didi

jsf - 如何在 ManagedBean 中注入(inject) CDI Bean?

转载 作者:行者123 更新时间:2023-12-04 13:33:09 25 4
gpt4 key购买 nike

我想使用注解@Inject 或@Produce 在ManagedBean 中注入(inject)一个CDI Bean。我使用的 CDI Bean 是:

@Named
@Startup
@ApplicationScoped
public class BaseBean {

private List<String> custs;

public List<String> getCusts() {
return custs;
}

public void setCusts(List<String> emps) {
this.custs = emps;
}

public BaseBean(){

}

@PostConstruct
void init() {
custs = new ArrayList<String>();
custs.add("Cust1");
custs.add("Cust3");
custs.add("Cust2");
custs.add("Cust4");
}

}

我要在其中注入(inject) CDI Bean 的 ManagedBean 是:
@SessionScoped
@ManagedBean
public class Hello implements Serializable {

@Inject
private BaseBean dBean;

private static final long serialVersionUID = 1L;
private List<String> customers;
private List<String> customersSelect;

public Hello() {
}

@PostConstruct
void init() {
// dBean = new BaseBean();
customers = dBean.getCusts();
}

public List<String> getCustomers() {
return customers;
}

public List<String> getCustomersSelect() {
return customersSelect;
}

public void setCustomersSelect(List<String> customersSelect) {
this.customersSelect = customersSelect;
}
}

然而,在 init 函数中,它会抛出 NullPointerException。如果我使用注解@Produces 而不是@Inject,结果是一样的:NullPointerException。 CDI Bean 有什么问题(不正确的注释)吗?我是否尝试以错误的方式注入(inject)它?我的代码缺少什么吗?我怎样才能让它工作?这是JSF代码:
<h:form id ="f">
<h:selectManyCheckbox layout="pageDirection" border="1" value="#{hello.customersSelect}">
<f:selectItems value="#{hello.customers}"></f:selectItems>
</h:selectManyCheckbox><br />
<h:commandButton action="response.xhtml" value="Click me" />
</h:form>

PS:如果我使用无状态 Bean 作为 BaseBean 并使用注解 @EJB 注入(inject)它,则它可以正常工作。

更新:我已经尝试使用注释 @SessionScoped ( javax.enterprise.context.SessionScoped ) 和 @NamedHello类(class)。虽然我没有收到 NullPointerException , h:selectManyCheckbox是空的。此外,让我印象深刻的是,当我添加 beans.xml META-INF 下的文件文件夹,我收到一个 StartException ,虽然文件应该在那里。我认为我的应用程序缺少能够进行依赖注入(inject)的正确配置。什么可能需要额外的配置?

更新 2:当我在 WEB-INF 文件夹中添加 beans.xml 文件时出现此错误。 beans.xml 文件是空的,它只包含以下行:
<?xml version="1.0" encoding="UTF-8"?> 

错误是:
Services which failed to start:      service jboss.deployment.unit."JSF1.war".PARSE: org.jboss.msc.service.StartException in service jboss.deployment.unit."JSF1.war".PARSE: Failed to process phase PARSE of deployment "JSF1.war"

12:51:11,482 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"jboss.deployment.unit.\"JSF1.war\".PARSE" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"JSF1.war\".PARSE: Failed to process phase PARSE of deployment \"JSF1.war\""}}}}

最佳答案

如果您使用 @Named,@patlov 的建议将起作用在您的 CDI bean 上。但是,如果您在支持 CDI 的环境中工作,不要使用 @ManagedBean .相反,一直使用 CDI。看到这个answer而且我相信您可以找到许多其他强烈建议您不要尝试做的事情。

只需从 javax.faces.bean.SessionScoped 切换至javax.enterprise.context.SessionScoped一切都会神奇地工作。您可能会遇到@ViewScoped 的缺失。但是,来自 CDI,在这种情况下,请使用 JBoss Seam 或 Apache Deltaspike 之类的工具来为您实现它。作为一个额外的好处,如果您已经为 JSF 编写了现有代码,它们还将自动将所有 JSF 范围替换为 CDI 范围。

更新:
这应该是 beans.xml 的内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

关于jsf - 如何在 ManagedBean 中注入(inject) CDI Bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15341877/

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