【问题标题】:MessageSource getMessage method not working in Spring mvcMessageSource getMessage方法在Spring mvc中不起作用
【发布时间】:2018-01-28 18:30:15
【问题描述】:

我开发了一个 Spring mvc 应用程序,我想在其中读取一些属性值 来自位于 weblogic 服务器内部的属性文件。

已执行以下步骤:

在以下路径中创建了一个项目特定文件夹 appConfig:Oracle/Middleware/ORACLE_HOME/user_projects/domains/wl_server/config/rmsConfig

将名为 commonConfig.properties 的属性文件放在其中。

还使用以下条目编辑了setDomainEnv.cmd

if NOT "%EXT_POST_CLASSPATH%"=="" (
   set EXT_POST_CLASSPATH=%EXT_POST_CLASSPATH%;%DOMAIN_HOME%\config\appConfig
if NOT "%POST_CLASSPATH%"=="" (
    set POST_CLASSPATH=%POST_CLASSPATH%;%EXT_POST_CLASSPATH%
) else (
    set POST_CLASSPATH=%EXT_POST_CLASSPATH%
 )
)

请在下面找到我的 Spring bean 配置文件:

  <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/util   http://www.springframework.org/schema/util/spring-util-4.3.xsd">

 <bean id="commonProps" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
      <property name="basename" value="commonConfig" />
      <property name="cacheSeconds" value="120" />
 </bean>
</beans>

在我的 Java Spring bean 类中,我这样引用它:

         @Component 
         public class LocationClient{
            @Autowired
            private MessageSource commonProps;

           public void showMessage(){
             System.out.println(commonProps.getMessage("common.line", null, null, null)); 
          }
       }

现在commonProps 不是 null 而是在控制台中打印 null 的“common.line”。

请在下面找到属性文件条目:

common.line=382

任何人都有任何合适的解决方案???

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    我认为您必须将属性文件添加到类路径或 WEB-INF 文件夹中。

    这是来自 Java Spring 文档的注释。

    对于典型的 Web 应用程序,消息文件可以放在 WEB-INF:例如“WEB-INF/messages”基本名称会找到一个 “WEB-INF/messages.properties”、“WEB-INF/messages_en.properties”等 安排以及“WEB-INF/messages.xml”, “WEB-INF/messages_en.xml”等。注意消息定义在一个 以前的资源包将覆盖以后包中的资源包,因为 顺序查找。

    更多信息请参考链接:ReloadableResourceBundleMessageSource

    【讨论】:

      【解决方案2】:

      一种可能的解决方案是使用ResourceBundleMessageSource 而不是ReloadableResourceBundleMessageSource 来定义您的属性,如下所示:

      <bean id="messageSource"
      class="org.springframework.context.support.ResourceBundleMessageSource">
              <property name="basename" value="commonConfig" />
          </bean>
      

      还要确保 commonConfig.properties 位于您的 WEB-INF 文件夹中,例如,如果您的应用程序是 appConfig,那么它应该位于类路径的根目录中:appConfig/WEB-INF/classes/commonConfig.properties

      第二种选择如果您需要在类路径之外使用commonConfig.properties,请使用您的第一种方法(确保 bean id 为 messageSource

       <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basename" value="file:/your/absoulute/path/commonConfig" />
            <property name="cacheSeconds" value="120" />
       </bean>
      

      【讨论】:

      • 有没有办法把属性文件放到应用服务器里面?
      • inside application server 意味着你需要classpath之外的properties文件?在那种情况下ReloadableResourceBundleMessageSource 让您定义一个特定的路径,无论它是否在类路径之外。另一方面ResourceBundleMessageSource 强制使用类路径资源。
      猜你喜欢
      • 2019-01-03
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多