【问题标题】:using ReloadableResourceBundleMessageSource in annotations injection在注解注入中使用 ReloadableResourceBundleMessageSource
【发布时间】:2012-11-07 08:09:48
【问题描述】:

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

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

【问题讨论】:

    标签: spring servlets annotations code-injection


    【解决方案1】:

    将 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 文件中进行配置。

    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>
    

    【讨论】:

    • 谢谢,但这不起作用 messageSource 一直返回 null
    • 已更新。如果您需要更多帮助,请告诉我。
    • 在使用 ReloadableResourceBundleMessageSource 时,不建议将属性文件存储在类路径中。它的文档说 - '由于应用程序服务器通常会缓存从类路径加载的所有文件,因此有必要将资源存储在其他地方(例如,在 Web 应用程序的“WEB-INF”目录中)。否则类路径中文件的更改将不会反映在应用程序中。'
    【解决方案2】:

    您是否在您的 servlet 中尝试过这个?

    private ApplicationContext getApplicationContext() {
        ApplicationContext wac = 
        WebApplicationContextUtils.getRequiredWebApplicationContext(
            this.getServletContext());
        return wac;
    } // getAppliationContext 
    

    然后,当您想要访问 bean 时:

    ReloadableResourceBundleMessageSource reloadableConfig = 
        (ReloadableResourceBundleMessageSource) 
            getApplicationContext().getBean("messageSource");
    

    【讨论】:

      【解决方案3】:

      下面是我目前公司项目的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 />
      

      我正在使用两个属性文件: messages_es.propertiesmessages_en.properties 用于语言。

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-06
        • 2020-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-13
        • 2011-09-15
        相关资源
        最近更新 更多