在注解注入中使用 ReloadableResourceBundleMessageSource

在注解注入中使用 ReloadableResourceBundleMessageSource

IT小君   2022-08-05T08:20:34

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

我的.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);

}

如何注入ReloadableResourceBundleMessageSourceusing 注释?

服务器费用不足...
评论(3)
IT小君

将 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>
2022-08-05T08:20:34   回复
IT小君

你在你的servlet中试过了吗?

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

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

ReloadableResourceBundleMessageSource reloadableConfig = 
    (ReloadableResourceBundleMessageSource) 
        getApplicationContext().getBean("messageSource");
2022-08-05T08:20:34   回复
IT小君

以下是我当前的公司项目 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语言。

我希望这能帮到您。

2022-08-05T08:20:34   回复