使用ReloadableResourceBundleMessageSource在注释注入 [英] using ReloadableResourceBundleMessageSource in annotations injection
问题描述
我使用 ReloadableResourceBundleMessageSource
在我的web项目,我注入类的一个servlet,问题是,我想使用Spring注解来注入类,但它不'吨似乎工作?我的code是:
I am using ReloadableResourceBundleMessageSource
in 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:
my.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
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("my.xml");
String message = applicationContext.getMessage(message, null, "Default
", null);
}
我如何注入 ReloadableResourceBundleMessageSource
使用注释?
推荐答案
更改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>
自动装配你的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);
}
}
目录结构
的src / main /资源/ myList.properties
test.prop=Hope this helps.
如果你不使用Spring MVC中,你可能需要启动应用程序时创建Spring应用程序上下文。这可以在你的web.xml文件中使用的ContextLoaderListener进行配置。
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.
在web.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>
这篇关于使用ReloadableResourceBundleMessageSource在注释注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!