2014-09-12 145 views
4

我試圖在我的控制器類中將靜態xml轉換爲POJO(解組)。我使用Jaxb2Marshaller,我配置以下列方式在我根上下文在Spring MVC控制器中注入Jaxb2Marshaller

<oxm:jaxb2-marshaller id="marshaller"> 
    <oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Airport"/>   
</oxm:jaxb2-marshaller> 

我試圖注入使用自動裝配編組。但它拋出沒有豆發現異常

@AutoWired 
private Unmarshaller marshaller; 

如何在控制器中注入編組器。任何其他方式或指出我的代碼中的錯誤將有所幫助?

+0

您是否在dispatcher-servlet.xml中配置了編組器? – 2014-09-12 12:15:10

+2

Spring中沒有'UnMarshaller'類...在Spring中有'Unmarshaller'類。所以我懷疑你只是使用了錯誤的類型。 – 2014-09-12 12:29:26

+0

@DanglingPiyush是的,我已經在dispatch-servlet.xml中配置了bean。現在我把它移到應用程序的上下文現在它的工作。謝謝 – karthik 2014-09-13 07:43:16

回答

1

Spring上下文配置

<?xml version="1.0" encoding="UTF-8" ?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:automation="http://www.springframework.org/schema/automation" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:oxm="http://www.springframework.org/schema/oxm" 
     xmlns:task="http://www.springframework.org/schema/task" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/util 
          http://www.springframework.org/schema/util/spring-util.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd 
          http://www.springframework.org/schema/automation 
          http://www.springframework.org/schema/automation/automation.xsd 
          http://www.springframework.org/schema/task 
          http://www.springframework.org/schema/task/spring-task.xsd 
          http://www.springframework.org/schema/oxm 
          http://www.springframework.org/schema/oxm/spring-oxm.xsd"> 

     <context:annotation-config/> 

     <oxm:jaxb2-marshaller id="jaxb2Marshaller"> 
      <oxm:class-to-be-bound name="your.package.Prova" /> 
     </oxm:jaxb2-marshaller> 

    </beans> 

@AutoWired 
private org.springframework.oxm.Unmarshaller jaxb2Marshaller; 
+0

感謝您的答案它的工作但爲什麼它不起作用,如果我在dispatcher-servlet.xml(我的意思是在servlet的上下文中)配置? – karthik 2014-09-13 07:45:11

+0

我認爲你應該看到這個[link](http://stackoverflow.com/a/16458969/3364187) – Xstian 2014-09-13 12:41:11

+0

有一點我不清楚:dispatcher-servlet.xml將創建一個單獨的spring app context (我認爲它是主要的子環境),並且在這個調度程序環境中,創建了jaxb2marshaller和控制器。他們都在相同的背景下,但爲什麼不能注入jaxb2marshaller? – 2014-11-24 02:27:32

2

我最近遇到了類似的問題,我相信解決方案,我想出是公認的答案,不同的是我認爲值得提出來供參考。

讓我確保我的理解您的情況是正確的:

在調度上下文,你必須:

  1. 創建JAXB2編組
  2. 創建的控制器。
  3. 在控制器的一個,你試圖注入編組(通過註釋)

但是注射失敗。當將JAXB2 Marshaller的創建移動到根上下文時,它將起作用。

如果我你的情況描述是正確的,那麼繼續閱讀:


您所做的一切,其實是正確的:JAXB2的Marshaller和控制器豆在相同的上下文中創建(dispatcher-上下文),Controller bean應該能夠使用編組bean來注入。

問題可能出在根上下文中,您創建了另一個控制器bean。並且它是在根上下文中額外的控制器bean不能用編組注入(因爲編組bean在存在於無法訪問的子上下文中)。因此,當你將marshaller bean移動到根上下文時,一切似乎都行得通(因爲在根上下文和調度程序上下文中的Controller都可以看到marshaller bean)

常見問題是在根上下文xml中聲明component-scan。默認情況下,component-scan將包含在其掃描期間使用@Controller註釋的類。這會在您的根上下文中引入另一個控制器bean。

如果是的情況下,解決方案是直接的:簡單地排除控制器在根上下文分量掃描(並且僅包括控制器在調度上下文分量掃描)

根上下文的xml:

<context:component-scan base-package="com.foo"> 
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan> 

調度程序上下文xml:

<context:component-scan base-package="com.foo" use-default-filters="false"> 
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan> 
+0

很好的解釋了阿德里安......我們可以用這個也作爲替代。 – karthik 2015-02-16 13:10:58

+0

就我個人而言,我認爲這不是一種「替代」,因爲我認爲接受的答案實際上是錯誤的 – 2015-02-16 13:53:48