【问题标题】:Reading properties file in Spring 3.2在 Spring 3.2 中读取属性文件
【发布时间】:2013-07-28 20:24:39
【问题描述】:

spring.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">  

  <bean id="meassageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="resource\message">  
    </property>
  </bean>

 </beans>

Main.java 类文件

public class Main {

    public static void main(String[] args) {

        ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");

        System.out.println(context.getMessage("emp", null, Locale.US));

    }

} 

我的属性文件位于 src/resource 文件夹中。文件名为 mesaage_en_US.properties。 我也尝试过使用不同的文件名,如 message.property、message_en.property 和不同的语言环境,如 Locale.English、Locale.UK,但没有运气。 我将属性文件移动到 src 文件夹,但得到了同样的异常。 我收到以下异常。

Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'emp' for locale 'en_US'.
    at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:65)
    at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1234)
    at org.beans.Main.main(Main.java:14)

请帮忙。

message_en_US.properties

emp=Hello Employee.

【问题讨论】:

  • 听起来你的ApplicationContext 没有自动检测你的MessageSource。 bean 名称中有一个错字 - 在您的实际代码中是这样吗?
  • 类路径属性应该用正斜杠分隔,所以试试资源/消息。
  • @superEb 实际代码没有错别字。
  • 您尝试过@samlewis 的建议吗?另外,如果您通过调用 ApplicationContext.getBean 然后调用 getMessage 来直接访问 messageSource bean 会怎样?

标签: java spring


【解决方案1】:

我喜欢为此使用PropertySourcesPlaceholderConfigurerHere's a great tutorial 让您开始。

基本上,您需要添加:

<context:property-placeholder location="classpath:foo.properties" />

到您的 spring xml 配置文件,其中“foo.properties”是资源在类路径中的绝对路径。

然后你可以将它们注入到这样的字段中:

@Value( "${jdbc.url}" )
private String jdbcUrl;

其中“jdbc.url”是属性文件中的引用名称。

当然,@Value 不能在你的static void main 中工作,但我真的怀疑static void main 是你想要使用你的属性的地方。您应该从 Spring Bean 访问它们。

【讨论】:

  • 对,但它的测试应用程序。
  • 好吧,我会断言,在制作 Spring 测试应用程序时,您应该按照使用 Spring 制作的方式制作。因此,您应该测试是否可以从 Spring 组件访问属性,而不是直接从应用程序上下文访问。 This question 的答案解释了为什么您不能在运行时从上下文中访问属性占位符。
  • 我也尝试过在不同的 bean 中实现 MessageSourceAware 接口。这也行不通。
  • 虽然这是对配置值的一个很好的建议,但它没有解决 i18n,这是MessageSource 的主要目的。
【解决方案2】:

我认为这是与this 问题重复的。基本上,它与你的包和代码中指定的语言环境不匹配有关。

【讨论】:

    【解决方案3】:

    我没有从ApllicationContext 获取消息,而是从MeassageSource 本身获取消息。我像这样改变了我的spring.xml

    <bean id="employee" class="org.bean.Employee" >
             <property name="id" value="1"/>
             <property name="name" value=""/>
             <property name="dept" value=""/>  
             <property name="messages" ref="messageSource"/>    
          </bean>
    

    现在我从Employee 班级呼叫messages.getMessage(this.messages.getMessage("emp", null, Locale.UK))。它的工作。

    【讨论】:

    • 请检查我的更新。您错误地将 bean 命名为 meassageSource(注意额外的“a”)。
    【解决方案4】:

    改成

    <property name="basename" value="message" />
    

    message_en_US.properties 与您的 spring.xml 在同一文件夹中。

    编辑在定义MessageSource 时,您的bean 名称中有错字。它的名字应该完全是messageSource。由于 meassageSource 中有额外的 a ApplicationContext 无法加载它。

    【讨论】:

    • @JitendraShukla 你可以尝试将道具文件移动到/src吗?
    • @JitendraShukla 您的 bean 名称中有错字。
    【解决方案5】:

    我已经重现了您的错误并找到了问题所在。这与 Spring 找不到捆绑包有关。我认为您应该在异常之前收到警告,并显示以下消息:

    WARNING: ResourceBundle [resource\message] not found for MessageSource: Can't find bundle for base name resource\message, locale en_US
    

    这是提示。该问题与您的项目结构以及在指定 setBasename 属性时如何搜索捆绑包有关。请看this

    无论如何,我认为你应该把你的包放在更标准的位置 src/main/resources。如果你遵循这个约定,你的 messageSource bean 应该这样定义:

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

    使用这种方法,您的示例应该会生成所需的行:

    你好,员工。

    【讨论】:

    • 是的,但是如果您收到来自ApplicationContext 的消息,那么它将不起作用。
    • @JitendraShukla 对不起,但它正在工作。我已经在一个测试项目中复制了您的代码,并且输出是预期的,没有错误。我从 ApplicationContext 收到消息。
    猜你喜欢
    • 2013-05-26
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 2018-09-28
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多