我正在构建一个基于Spring Boot的Spring独立应用程序。我希望此应用程序从位于单独目录中的jar文件外部的属性文件中读取其属性。生成的应用程序的结构如下所示
testApplication
├── test-1.0-SNAPSHOT.jar
├── lib
│ └── <dependencies are here>
└── conf
└── application.properties
我想在类路径上加载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)
我尝试通过此Class注释从应用程序读取文件:
@PropertySource("classpath:application.properties")
Java文档没有提及,可以在类路径上加载非jar文件,但是如果此application.properties位于jar文件test-1.0-SNAPSHOT.jar中,则可以通过这种方式对其进行访问。
是否可以将外部非jar文件放在classpath上?我知道我不一定在classpath上有文件,我可以通过不同的方式访问它,但是我仍然想知道是否有可能。
最佳答案
尝试
java -cp "./*;lib/*;conf" com.myapp.Test
Asterix(*)用于查找所有JAR而非类。