在注解注入中使用 ReloadableResourceBundleMessageSource [英] using ReloadableResourceBundleMessageSource in annotations injection

查看:40
本文介绍了在注解注入中使用 ReloadableResourceBundleMessageSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 web 项目中使用 ReloadableResourceBundleMessageSource,并将类注入到 servlet,问题是我想使用 Spring 注释注入类,但它似乎不起作用?我的代码是:

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/resources/myList.properties

test.prop=Hope this helps.

如果您不使用 Spring MVC,您可能需要在启动应用程序时创建一个 Spring 应用程序上下文.这可以使用 ContextLoaderListener 在您的 Web.xml 文件中进行配置.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆