我想MessageSource
在 Spring 中使用依赖注入来初始化我的字段。这是到目前为止:
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>
我已经定义了我的 messageSourceroot-context.xml
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
我的控制器类工作正常,但是我无法在PdfRevenueReportView
类中注入 messageSource 字段。我应该怎么做才能使 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 总是 null ?