2013-03-28 113 views
1

我想在Spring中使用依赖注入初始化我的MessageSource字段。这是迄今为止:Spring MVC中的依赖注入

package com.ucmas.cms.view; 

@Component 
public class PdfRevenueReportView extends AbstractPdfView { 
    ... 
    @Autowired 
    private MessageSource messageSource; 
    ... 
} 

mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="sec://www.springframework.org/schema/mvc" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <context:component-scan base-package="com.ucmas.cms.controller,com.ucmas.cms.view" /> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <mvc:annotation-driven /> 
    ... 
    <beans:bean class="org.springframework.web.servlet.view.XmlViewResolver"> 
     <beans:property name="location" value="/WEB-INF/spring-pdf-views.xml" /> 
     <beans:property name="order" value="0" /> 
    </beans:bean> 

</beans:beans> 

我在root-context.xml

<bean id="messageSource"  

class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:messages" /> 
     <property name="defaultEncoding" value="UTF-8" /> 
    </bean> 

我的控制器类做工精细定义我为messageSource,但是我无法注入为messageSource字段在PdfRevenueReportView类中。我应该怎么做才能使DI工作?

修订

我在XML中定义的视图遵循

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 
    <bean id="PdfRevenueSummary" class="com.ucmas.cms.view.PdfRevenueReportView" /> 
</beans> 

也许这就是为什么为messageSource总是空?

+0

有你的地方定义为MessageSource豆? – Simeon 2013-03-28 09:21:07

+0

yes在我的'root-context.xml'中 – abiieez 2013-03-28 09:23:30

+0

根据这个配置,应该有一个名为'messages'的文件,在你的类路径中没有扩展名。有这样的文件吗? – Simeon 2013-03-28 09:46:28

回答

2

我得到它的工作通过更新我的弹簧PDF-views.xml到

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 
    <bean id="PdfRevenueSummary" class="com.ucmas.cms.view.PdfRevenueReportView"> 
     <property name="messageSource" ref="messageSource"/> 
    </bean> 
</beans> 

然而,这需要我来产生为messageSource场setter和getter和取出@Autowired注解。

0

在您的dispatcher servlets配置中,您需要为消息源创建一个bean。

<bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="directory/with/messagesource"/> 
</bean> 

还要确保messages目录在类路径上。

+0

我已经定义了,实际上可以在我的控制器类中检索消息源。 – abiieez 2013-03-28 09:25:34

+0

@abiieez我不确定这是否重要,但我已经在配置文件中为调度程序servlet而不是应用程序上下文定义了我的消息源。我认为有可能是控制器无法在应用程序配置中看到bean的问题。 – 2013-03-28 09:32:51

0

您正在使用Autowired注释messageSource。当您的bean messageSource虽然是MessageResource的接口,但实际上是实现ResourceBundleMessageSource时,这可能不起作用。

我觉得如果你使用@资源,而不是因为它是基于域名的DI会工作:

@Resource 
private MessageSource messageSource;