在类路径上添加属性文件
Add properties file on classpath
我正在构建一个基于 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 文件放在类路径中?我知道我不一定在类路径上有文件,我可以通过不同的方式访问它,但我仍然很好奇它是否可能。
尝试
java -cp "./*;lib/*;conf" com.myapp.Test
Asterix (*) 用于查找除 类 之外的所有 JAR。
抱歉,删除 "conf/" 后的星号 (*)。
我正在构建一个基于 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 文件放在类路径中?我知道我不一定在类路径上有文件,我可以通过不同的方式访问它,但我仍然很好奇它是否可能。
尝试
java -cp "./*;lib/*;conf" com.myapp.Test
Asterix (*) 用于查找除 类 之外的所有 JAR。
抱歉,删除 "conf/" 后的星号 (*)。