1

MessageSourceSpring で Dependency Injection を使用してフィールドを初期化したいと考えています。これはこれまでのものです:

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 フィールドを挿入できません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 が常に 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 フィールドのセッターとゲッターを生成し、@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 に答える