[英]Dependency Injection in Spring MVC
I would like to initialize my MessageSource
field with Dependency Injection in Spring. 我想在Spring中使用Dependency Injection初始化我的
MessageSource
字段。 This is what have so far: 到目前为止,这是什么:
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>
I have defined my messageSource in root-context.xml
我已经在
root-context.xml
定义了messageSource
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
My controller classes work fine, however I am unable to inject the messageSource field in the PdfRevenueReportView
class. 我的控制器类工作正常,但是我无法在
PdfRevenueReportView
类中注入messageSource字段。 What should I do to make the DI works ? 如何使DI起作用?
UPDATED 更新
I define the view in xml as follow 我在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>
Perhaps this is why the messageSource is always null ? 也许这就是为什么messageSource始终为null的原因?
I got it working by updating my spring-pdf-views.xml into 我通过将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>
However this requires me to generate setter and getter for messageSource field and removing the @Autowired
annotation. 但是,这需要我为messageSource字段生成setter和getter并删除
@Autowired
批注。
In your dispatcher servlets
configuration you need to create a bean for the message source. 在
dispatcher servlets
配置中,您需要为消息源创建一个bean。
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="directory/with/messagesource"/>
</bean>
Also ensure that the messages
directory is on the classpath. 还要确保
messages
目录位于类路径上。
You are annotating messageSource with Autowired. 您正在使用自动装配来注释messageSource。 This might not work when your bean messageSource whilst is an interface of MessageResource, is really the implementation ResourceBundleMessageSource .
当您的bean messageSource虽然是MessageResource的接口,实际上是ResourceBundleMessageSource的实现时,这可能不起作用。
I think your DI would have worked if you used @Resource instead as it is name based: 我认为如果您使用@Resource而不是您的DI会起作用,因为它是基于名称的:
@Resource
private MessageSource messageSource;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.