【问题标题】:Loading a .properties file with Spring (Java)使用 Spring (Java) 加载 .properties 文件
【发布时间】:2013-07-01 15:03:03
【问题描述】:

我在加载 .properties 文件时遇到问题。

该文件名为“businessmessages_en_US.properties”,存储在“src/main/resources/config/i18n”中。我在构建路径中添加了“src/main/resources”,并在我的 spring xml 中创建了一个 bean:

<bean name="messageResource" id="messageResource"
    class="[package].CustomResourceBundleMessageSourceImpl">
    <property name="basename">
        <value>classpath*:config/i18n/*.properties</value>
    </property>
</bean>

注意“basename”属性根据类必须是字符串

然而,每次我运行我的程序时,记录器都会发送一条消息:

01 Jul 2013 09:12:34,267 WARN [package].CustomResourceBundleMessageSourceImpl - ResourceBundle [classpath*:config/i18n/*.properties] not found for MessageSource: Can't find bundle for base name classpath*:config/i18n/*.properties, locale en_US 

我需要程序来读取这个文件,以便记录器可以有正确的值。任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: java spring properties javabeans


    【解决方案1】:
    <context:property-placeholder location="classpath*:*.properties"/>
    

    然后像这样使用它:

        <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
            <property name="driverClassName" value="${database.driverClassName}"/>
            <property name="url" value="${database.url}"/>
            <property name="username" value="${database.username}"/>
            <property name="password" value="${database.password}"/>
        </bean>
    

    【讨论】:

    • 我不确定我是否不完全了解如何实施此解决方案,或者它是否不适合我。我刚刚在我的问题中添加了一条注释,该值需要是一个字符串,因为将读取它的类需要该类型。
    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2020-12-02
    • 1970-01-01
    • 2014-06-29
    • 2014-11-28
    • 1970-01-01
    相关资源
    最近更新 更多