2013-12-11 50 views
0

我在Spring中面臨DI的問題。我的應用程序集成了PrimeFaces & Spring,並使用Spring for for DI和創建託管bean。@Value註解和空指針異常

我有一個<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. 

} 

二豆 - 現在的問題是:

@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. 

我唯一想要的就是繼續引用DefinicjeParametrow instan從另一個bean(這是我選擇的行)的ce。我試圖改變我的豆的範圍,但它沒有奏效。我嘗試的第二件事是使用@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。

回答

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與春季(反之亦然)的整合,所以我不能保證解決方案,但根本原因是一個以上解釋。

+0

你真棒!它非常完美,非常簡單!如果你曾經在波蘭只是打電話給我 - 我必須給你買一瓶我們最好的波蘭伏特加;) –