using ReloadableResourceBundleMessageSource in annotations injection

北城余情 提交于 2019-12-23 13:09:03

问题


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);

}

How do I inject the ReloadableResourceBundleMessageSource using annotations?


回答1:


Change the bean to autowire by name

<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

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

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.

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>



回答2:


Did you try this in your servlet?

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

Then, when you want to gain access to the bean:

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



回答3:


below is my current company project 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.properties and messages_en.properties for language.

I hope this will help you.



来源:https://stackoverflow.com/questions/13265545/using-reloadableresourcebundlemessagesource-in-annotations-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!