我在 Spring 中遇到了 DI 问题。我的应用程序集成了 PrimeFaces 和 Spring,并使用 Spring 进行 DI 和创建托管 bean。
我有一个 <p:datatable>
当用户选择一行并按下按钮时,我的应用程序会打开一个动态对话框,其中包含另一个网格以显示所选行。我想为每个网格设置两个单独的托管 bean。
我的 .jsf 页面非常简单 - 只是一个 <p:datatable>
和延迟加载 <p:dialog>
:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:body>
<ui:composition template="../templates/layout.xhtml">
<ui:define name="title">
PrimeFaces webPage
</ui:define>
<ui:define name="content">
<h:form style="height:90%;">
<p:dataTable value="#{parametrySystemoweGridBean.definicjeParametrowModel}" var="definicjeParametrow" id="definicjeParametrowGrid"
rowKey="#{definicjeParametrow.id}" paginator="true" rows="10" filteredValue="#{parametrySystemoweGridBean.filteredDefinicjeParametrowList}"
selection="#{parametrySystemoweGridBean.definicjaParametruSelected}" rowsPerPageTemplate="5, 10, 15, 20"
selectionMode="single" scrollHeight="100%" scrollable="true">
<!-- Columns -->
<f:facet name="footer">
<p:commandButton value="Wartości parametru" onclick="PF('wartosciDialog').show();">
</p:commandButton>
<p:commandButton value="Odśwież" actionListener="#{parametrySystemoweGridBean.refresh}" update="definicjeParametrowGrid">
</p:commandButton>
</f:facet>
</p:dataTable>
</h:form>
<p:dialog widgetVar="wartosciDialog" header="Wartości parametru #{parametrySystemoweGridBean.definicjaParametruSelected.id}"
appendTo="@(body)" dynamic="true">
<h:form style="height:90%;">
<p:dataTable id="wartosciGrid" var="wartoscParametru" selectionMode="single" scrollHeight="100%" scrollable="true"
value="#{wartosciParametruGridBean.wartosciParametrowModel}" rendered="true">
<!-- Columns -->
</p:dataTable>
</h:form>
</p:dialog>
</ui:define>
</ui:composition>
</h:body>
</html>
第一个 bean :
@Component("parametrySystemoweGridBean")
@Scope("request")
public class ParametrySystemoweGridBean {
private DefinicjeParametrow definicjaParametruSelected;
public ParametrySystemoweGridBean() {
}
//Some other stuff - getters, setters etc.
}
第二个 bean - 这是问题所在:
@Component("wartosciParametruGridBean")
@Scope("request")
public class WartosciParametruGridBean {
private final Logger log = Logger.getLogger(WartosciParametruGridBean.class);
private WartosciParametrowModel wartosciParametrowModel;
private WartosciParametrow selected;
@Value("#{parametrySystemoweGridBean.definicjaParametruSelected}")
private DefinicjeParametrow definicjeParametrow;
public WartosciParametruGridBean() {
this.log.info(this.definicjeParametrow.getKod());
}
//Some other stuff - getters, setters etc.
我唯一想要的是保持对另一个 bean(这是我选择的行)的 DefinicjeParametrow 实例的引用。我试图改变我的 beans 范围,但没有成功。我尝试的第二件事是使用 @Autowired
Autowiring 整个 WartosciParametruGridBean。但它也失败了。
显示对话框时出现以下异常:
SEVERE: Error Rendering View[/pages/parametry.xhtml]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wartosciParametruGridBean' defined in file [...]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [pl.sygnity.cbop.admin.web.beans.WartosciParametruGridBean]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007)
...
拜托,你能帮我做这件事吗?我真的被困住了,是 Spring 的新手。我正在使用最新的 Primefaces 4.0、JSF Mojarra 2.2.4 和 Spring 3.2.5.RELEASE。
最佳答案
在 Spring 可以向其中注入(inject)任何值之前,需要先构造该对象。
那么在这一步
public WartosciParametruGridBean() {
this.log.info(this.definicjeParametrow.getKod());
}
字段 definicjeParametrow
仍然是 null
。您始终可以将 parametrySystemoweGridBean 直接 Autowiring 到您的构造函数中
@Autowired
public WartosciParametruGridBean(ParametrySystemoweGridBean parametrySystemoweGridBean) {
this.log.info(this.parametrySystemoweGridBean.getDefinicjeParametrow().getKod());
}
我不知道 JSF 与 Spring 的集成(反之亦然),所以我不能保证解决方案,但根本原因是上面解释的那个。
关于java - @Value 注解 & NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20530662/