Spring Boot 2.5.4独立应用外置application.properties失败求助
Spring Boot 2.5.4外置配置文件启动失败排查与解决
问题描述
在Spring Boot 2.5.4独立非Web应用中,尝试将application.properties外置到Jar包外,使用以下启动命令:
java -jar app-1.0.jar --spring.config.location=file:D:\config\application.properties
启动时抛出错误:
Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
即使设置环境变量:
set SPRING_CONFIG_NAME=application set SPRING_CONFIG_LOCATION=file:D:\config\application.properties
问题仍未解决,完整报错日志如下:
2023-07-12 21:34:40.984 INFO 8604 --- [ main] com.s7.SchedulerApplication : Starting SchedulerApplication v2.1.4 using Java 11.0.16.1 on DESKTOP-6NOAC01 with PID 8604 (C:\Users\app\IdeaProjects\scheduler-app\target\scheduler-app.2.1.4.jar started by app in C:\Users\app\IdeaProjects\scheduler-app\target) 2023-07-12 21:34:41.028 INFO 8604 --- [ main] com.s7.SchedulerApplication : No active profile set, falling back to default profiles: default 2023-07-12 21:34:41.435 WARN 8604 --- [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.s7.SchedulerApplication]; nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist 2023-07-12 21:34:41.617 ERROR 8604 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.s7.SchedulerApplication]; 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:189) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:64) ~[spring-boot-2.5.4.jar!/:2.5.4] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.4.jar!/:2.5.4] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.4.jar!/:2.5.4] at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.4.jar!/:2.5.4] at com.s7.SchedulerApplication.main(SchedulerApplication.java:35) ~[classes!/:2.1.4] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[scheduler-app.2.1.4.jar:2.1.4] at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[scheduler-app.2.1.4.jar:2.1.4] at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[scheduler-app.2.1.4.jar:2.1.4] at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[scheduler-app.2.1.4.jar:2.1.4] 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:187) ~[spring-core-5.3.9.jar!/:5.3.9] at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159) ~[spring-core-5.3.9.jar!/:5.3.9] at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:110) ~[spring-core-5.3.9.jar!/:5.3.9] at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:81) ~[spring-core-5.3.9.jar!/:5.3.9] at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:67) ~[spring-core-5.3.9.jar!/:5.3.9] at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:67) ~[spring-core-5.3.9.jar!/:5.3.9] at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:37) ~[spring-core-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:463) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:280) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:304) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207) ~[spring-context-5.3.9.jar!/:5.3.9] at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175) ~[spring-context-5.3.9.jar!/:5.3.9] ... 19 common frames omitted
排查方向与解决方案
1. 移除代码中硬编码的classpath配置加载
从报错栈可以看到,异常来自ConfigurationClassParser.processPropertySource,说明你的主配置类(如SchedulerApplication)可能通过@PropertySource注解强制加载classpath下的application.properties,示例代码如下:
@PropertySource("classpath:application.properties") @SpringBootApplication public class SchedulerApplication { // ... }
这种情况下,即使指定外部配置路径,Spring依然会优先尝试加载classpath下的文件,导致报错。解决方式:
- 直接移除
@PropertySource注解,Spring Boot会自动根据spring.config.location参数加载外部配置 - 或者修改注解为动态路径,配合启动参数传递外部配置位置:
启动命令改为:@PropertySource(value = "${external.config.path:classpath:application.properties}")java -jar app-1.0.jar --external.config.path=file:D:/config/application.properties
2. 修正spring.config.location参数格式
Windows系统下路径中的冒号容易引发解析问题,建议用双引号包裹路径,或者将反斜杠改为正斜杠:
java -jar app-1.0.jar --spring.config.location="file:D:/config/application.properties"
如果需要保留默认配置加载路径(如classpath),可以用逗号分隔多个路径:
java -jar app-1.0.jar --spring.config.location="classpath:/,file:D:/config/application.properties"
3. 检查Jar包内是否存在配置文件
如果Jar包中没有application.properties,但代码依赖它加载,就会报错。用以下命令查看Jar包内容:
jar tf scheduler-app.2.1.4.jar | grep application.properties
如果无输出,说明Jar包中不存在该文件,此时要么移除硬编码的@PropertySource,要么确保外部路径的配置文件存在且可读取。
4. 使用spring.config.additional-location替代spring.config.location
spring.config.location会覆盖默认配置加载路径,而spring.config.additional-location是在默认路径之外额外添加外部配置,即使外部配置不存在,也会尝试加载默认路径的配置,避免报错:
java -jar app-1.0.jar --spring.config.additional-location=file:D:/config/application.properties
5. 验证外部配置文件的路径与权限
- 确认
D:\config\application.properties文件真实存在,路径拼写无误(Windows不区分大小写,但建议保持一致) - 确保启动程序的用户拥有该文件的读取权限
- 可以尝试将配置文件放在Jar包同级目录,使用相对路径简化配置:
java -jar app-1.0.jar --spring.config.location=./application.properties
内容的提问来源于stack exchange,提问作者Vijaya Kumar
相关产品推荐
相关产品推荐

