spring 在注解注入中使用 ReloadableResourceBundleMessageSource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13265545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using ReloadableResourceBundleMessageSource in annotations injection
提问by Yo Al
I am using ReloadableResourceBundleMessageSourcein my web project, and I inject the class to a servlet, the problem is that I want to inject the class using Spring annotations but it doesn't seem to work? My code is:
我ReloadableResourceBundleMessageSource在我的 web 项目中使用,并将类注入到 servlet,问题是我想使用 Spring 注释注入类,但它似乎不起作用?我的代码是:
my.xml
我的.xml
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:myList</value>
</list>
</property>
<property name="cacheSeconds" value="1"/>
</bean>
myServletClass.java
myServletClass.java
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("my.xml");
String message = applicationContext.getMessage(message, null, "Default
", null);
}
How do I inject the ReloadableResourceBundleMessageSourceusing annotations?
如何注入ReloadableResourceBundleMessageSourceusing 注释?
回答by Kevin Bowersox
Change the bean to autowire by name
将 bean 更改为按名称自动装配
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
autowire="byName">
<property name="basenames">
<list>
<value>classpath:myList</value>
</list>
</property>
<property name="cacheSeconds" value="1"/>
</bean>
Autowire the Message Source within your Servlet
在 Servlet 中自动装配消息源
public Class MyServlet extends HttpServlet{
@Autowired
MessageSource messageSource;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String message = messageSource.getMessage("test.prop", null, "Default",null);
}
}
Directory Structure
目录结构


src/main/resources/myList.properties
src/main/resources/myList.properties
test.prop=Hope this helps.
If you are not using Spring MVC you may need to create a Spring application context when starting your application. This can be configured in your Web.xml file using the ContextLoaderListener.
如果您没有使用 Spring MVC,您可能需要在启动应用程序时创建一个 Spring 应用程序上下文。这可以使用 ContextLoaderListener 在您的 Web.xml 文件中进行配置。
Web.xml
网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
回答by user1955401
Did you try this in your servlet?
你在你的servlet中试过这个吗?
private ApplicationContext getApplicationContext() {
ApplicationContext wac =
WebApplicationContextUtils.getRequiredWebApplicationContext(
this.getServletContext());
return wac;
} // getAppliationContext
Then, when you want to gain access to the bean:
然后,当您想要访问 bean 时:
ReloadableResourceBundleMessageSource reloadableConfig =
(ReloadableResourceBundleMessageSource)
getApplicationContext().getBean("messageSource");
回答by ankit
below is my current company project xml:
以下是我目前的公司项目 xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="net.xyz.controller" />
<bean id="ViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<mvc:resources location="/assets/" mapping="/assets/**" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="messageSource" class=
"org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*/*" />
<bean class="net.xyz.controller.InterceptorController" />
</mvc:interceptor>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
<mvc:annotation-driven />
and I am using two properties file:
messages_es.propertiesand messages_en.propertiesfor language.
我正在使用两个属性文件:
messages_es.properties和messages_en.properties语言。
I hope this will help you.
我希望这能帮到您。

