【问题标题】:how to set properties file under classpath in spring using sts如何在spring中使用sts在类路径下设置属性文件
【发布时间】:2014-02-01 14:59:08
【问题描述】:

我正在学习 spring 并试图了解从属性文件中检索值。

我正在尝试以下方式来尝试一些示例

<util:properties id="spelProp" location="classpath:/META-INF/spelProperties.properties"></util:properties>

spelProperties.properties 的内容在哪里

spelTeacher.firstName="First name from Properties"
spelTeacher.lastName="Last name from properties"

我尝试访问 lastName 以便设置 bean 属性之一,例如

<bean id="spelTeacher3Xml" class="com.learningweb.Service.SpelSampleTeacher">
<property name="lastName" value="#{spelProp[spelTeacher.lastName]}" />
</bean>

当我尝试使用 STS 运行时 我收到一个错误 “/META-INF/spelProperties.properties”不存在。

谁能帮我理解这里出了什么问题。 我是否错过了配置类路径或任何 Spring 框架相关设置的任何项目?

感谢您的帮助 (此外,任何理解“类路径:”的参考都会有所帮助。我试图搜索,但我得到了我正在寻找的正确材料。我试图了解类路径:在春季做什么) 谢谢!!!

【问题讨论】:

    标签: spring-mvc properties spring-data


    【解决方案1】:

    您可能想尝试这样的方法:

        <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="locations">  
                <list>  
                    <value>classpath:/META-INF/spelProperties.properties</value>  
                </list>  
            </property>  
        </bean>
    

    或

        <context:annotation-config/>
        <context:property-placeholder location="classpath*:*.properties" />
    

    然后在 spelTeacher3Xml 中访问它,例如:value="${spelTeacher.lastName}"

    根据您的 Spring 版本,查看PropertyPlaceholderConfigurer 的 API 或其他更专业的版本。

    This guys site有很多基本的Spring配置教程。不过,它们可能有点过时了。

    【讨论】:

    • 感谢您的建议。我有一个关于使用 ${} 的问题,我读到 SPel 实际上是 #{}。两者都一样吗?
    • 在大多数情况下,它们对于给定的目的都是可以接受的,SpringEL 只是有更多的能力。 See this question
    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    相关资源
    最近更新 更多