@Value注释&空指针异常

时间:2013-12-11 21:52:09

标签: java spring jsf primefaces annotations

我在Spring遇到DI问题。我的应用程序集成了PrimeFaces& Spring并使用Spring for 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>

第一个豆:

@Component("parametrySystemoweGridBean")
@Scope("request")
public class ParametrySystemoweGridBean {

    private DefinicjeParametrow definicjaParametruSelected;

    public ParametrySystemoweGridBean() {
    }

    //Some other stuff - getters, setters etc.

}

第二个豆 - 这就是问题所在:

@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实例。我试图改变我的bean范围,但它没有奏效。我尝试的第二件事是使用@Autowired自动装载整个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)
...

拜托,你能帮我吗?我真的被困在春天的新手。我正在使用最新的Primefaces 4.0,JSF Mojarra 2.2.4和Spring 3.2.5.RELEASE。

1 个答案:

答案 0 :(得分:2)

在Spring可以向其中注入任何值之前,需要构造对象。

所以在这一步

public WartosciParametruGridBean() {
    this.log.info(this.definicjeParametrow.getKod());
}

字段definicjeParametrow仍为null。您始终可以将parametrySystemoweGridBean直接自动装配到构造函数

@Autowired
public WartosciParametruGridBean(ParametrySystemoweGridBean parametrySystemoweGridBean) {
    this.log.info(this.parametrySystemoweGridBean.getDefinicjeParametrow().getKod());
}

我不知道JSF与Spring的集成(反之亦然),所以我不能保证解决方案,但根本原因是上面解释的那个。