1

我想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 ?

4

3 回答 3

2

我通过将 spring-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注释。

于 2013-03-29T01:39:07.623 回答
0

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

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

还要确保该messages目录位于类路径中。

于 2013-03-28T09:23:47.730 回答
0

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

我认为如果您使用 @Resource 代替,您的 DI 会起作用,因为它是基于名称的:

@Resource
private MessageSource messageSource;
于 2013-07-12T00:24:07.563 回答