我想在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总是空?
有你的地方定义为MessageSource豆? – Simeon 2013-03-28 09:21:07
yes在我的'root-context.xml'中 – abiieez 2013-03-28 09:23:30
根据这个配置,应该有一个名为'messages'的文件,在你的类路径中没有扩展名。有这样的文件吗? – Simeon 2013-03-28 09:46:28