我正在构建一个基于
Spring Boot的
Spring独立应用程序.我希望这个应用程序从一个独立目录中的jar文件之外的属性文件中读取它的属性.构建的应用程序的结构如下所示
testApplication
├── test-1.0-SNAPSHOT.jar
├── lib
│ └──
└── 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上有文件,我可以通过不同的方式访问它,但我仍然很好奇,如果可能的话.