2015-10-20 185 views
0

我正在使用Spring Boot並試圖讓我的應用程序能夠在運行時獲取配置文件。下面是我在春天文件spring classpath在WEB-INF/classes下找不到配置文件

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

但是當我開始

Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist 

我試圖將其更改爲

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

我得到了不同的服務,我得到這個錯誤錯誤

Could not resolve placeholder 'service.name' in string value "${service.name}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'service.name' in string value "${service.name}" 

看起來像我牛逼能找到該文件,但無法找到字符串(這是內部config.properties)

我把我的config.properties路徑下的WEB-INF /班/,WAR文件裏

788 Mon Oct 19 09:54:28 PDT 2015 WEB-INF/classes/config.properties 

任何人有任何想法出了什麼問題?

在此先感謝!

+0

添加您的'config.properties'代碼到您的文章,我們會知道你是如何試圖加載config.properties,RU使用'PropertyPlaceholderConfigurer' – Uppicharla

回答

0

WEB-INF/classes是我希望在舊的WAR風格的應用程序中找到類路徑資源而不是Spring Boot應用程序的地方。你應該能夠通過將config.properties放置在src/main/resources中來解決你的問題,而這是Maven或Gradle中的默認位置(這是Spring Boot非常流行的構建工具,並且我懷疑你正在使用其中一個或另一個)。

第二 - 我會建議你自己不配置你的配置的位置,只是使用Spring Boot默認位置是src/main/resources/application.properties在這個位置尋找配置文件是一個約定,應該工作沒有進一步的配置(除了刪除你當前的屬性配置)。

請參閱23.3 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

+0

謝謝!現在我清楚了。 – feiwhoamI

0

SpringBoot默認情況下會尋找application.properties從classpath,你有你的類路徑application.properties?如果是,則在application.properties中聲明一個屬性spring.config.location=classpath:/config.properties

如果您缺省沒有application.properties文件,請在啓動服務器時通過命令行參數傳遞您的配置文件的位置,如下所示。

$ java -jar myproject.jar --spring.config.location=classpath:/cofnig.properties 

對於Spring配置的詳細信息,請參閱SpringExternalizeConfig

+0

謝謝!現在我清楚了。 – feiwhoamI

相關問題