簡體   English   中英

如何使用sts在spring中設置classpath下的屬性文件

[英]how to set properties file under classpath in spring using sts

我正在學習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”不存在的錯誤。

有人可以幫我理解這里有什么問題。 我是否錯過了配置classpath或任何Spring框架相關設置的任何項目?

感謝你的幫助(也有任何參考,了解“classpath:”會有所幫助。我試圖搜索,但我得到了正在尋找的正確材料。我想了解classpath:春天做了什么)謝謝!!!

您可能想要嘗試這樣的事情:

    <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或其他一些更專業的版本。

這個人的網站有很多基本的Spring配置教程。 不過,他們可能有點過時了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM