簡體   English   中英

關於Spring上下文配置文件中類路徑的用法

[英]About the usage of classpath in spring context config file

在spring上下文配置中,我想在Web項目的路徑中傳遞一些文件: app/src/main/resources ,我使用"classpath"標記將文件作為構造函數參數傳遞到Bean類中:

<beans:bean id="beanId" class="com.sample.class">
    <beans constructor-arg value="classpath:test.properties"/>
</beans:bean>

在Bean中,當它在其構造函數中接收到此參數時,它將打印出值以查看是否傳遞了文件的實際路徑。但是當我運行代碼時,它只是直接打印出"classpath:test.properties"而不是實際的路徑,因此我無法獲得正確的值來進行進一步的處理,有人可以對此提供一些建議嗎? 提前致謝。

如果要在所需的bean中使用屬性值,請執行以下操作

@Value("${appConf.testValue}")
String testValue;

不必將屬性文件傳遞給bean,但是要執行此操作,應為Spring上下文加載屬性文件,為此您必須使用

<bean id="viewPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:test.properties</value>
    </property>
 </bean>

暫無
暫無

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

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