簡體   English   中英

在類路徑上添加屬性文件

[英]Add properties file on classpath

我正在構建一個基於Spring Boot的Spring獨立應用程序。 我希望這個應用程序從一個獨立目錄中的jar文件之外的屬性文件中讀取它的屬性。 構建的應用程序的結構如下所示

testApplication
├── test-1.0-SNAPSHOT.jar
├── lib
│   └── <dependencies are here>
└── conf
    └── application.properties

我想在classpath上加載application.properties文件,所以我能夠在我的應用程序中讀取。 可能嗎? 因為當我像這樣運行我的jar文件時(在Windows上):

java -cp "./*;lib/*;conf/*" com.myapp.Test

我得到以下的例外:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.myapp.Test]; nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180)
...
Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist       
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)

我嘗試通過此類注釋從應用程序中讀取文件:

@PropertySource("classpath:application.properties")

Java文檔沒有提到,可以在類路徑上加載非jar文件,但如果這個application.properties在jar文件test-1.0-SNAPSHOT.jar中,則可以通過這種方式訪問​​它。

是否可以將外部非jar文件放在類路徑上? 我知道我不一定在classpath上有文件,我可以通過不同的方式訪問它,但我仍然很好奇,如果可能的話。

嘗試

java -cp "./*;lib/*;conf" com.myapp.Test

Asterix(*)用於查找所有JAR而不是類。

抱歉,在“conf /”之后刪除星號(*)。

暫無
暫無

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

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