Windows Docker镜像中Spring Boot分层Jar加载外部配置报错解决
Spring Boot分层Jar在Windows Docker容器中启动时FileNotFoundException问题解决
问题描述
运行基于Windows Docker镜像的Spring Boot分层Jar应用时,启动容器后触发FileNotFoundException,提示无法找到classpath下的SVMSController.properties。已将主机C:\my-app-props目录挂载到容器内C:/application/BOOT-INF/classes/config/路径,且确认容器内文件存在;Dockerfile中指定了--spring.config.location参数,应用代码通过@PropertySource注解引用配置文件,但仍报错。
报错日志
2022-09-06 13:03:15.746 INFO 1412 --- [ main] c.m.cdmf.svms.SVMSControllerApplication : Starting SVMSControllerApplication v3.3.2-SNAPSHOT using Java 1.8.0_212 on 9aff754dd15d with PID 1412 (C:\application\BOOT-INF\classes started by ContainerAdministrator in C:\application) 2022-09-06T12:03:15.767885300Z 2022-09-06 13:03:15.746 INFO 1412 --- [ main] c.m.cdmf.svms.SVMSControllerApplication : No active profile set, falling back to 1 default profile: "default" 2022-09-06T12:03:15.872396900Z 2022-09-06 13:03:15.855 WARN 1412 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.mottmac.cdmf.svms.SVMSControllerApplication]; nested exception is java.io.FileNotFoundException: class path resource [SVMSController.properties] cannot be opened because it does not exist 2022-09-06T12:03:15.911392600Z 2022-09-06 13:03:15.887 ERROR 1412 --- [ main] o.s.boot.SpringApplication : Application run failed 2022-09-06T12:03:15.911392600Z 2022-09-06T12:03:15.911392600Z org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.mottmac.cdmf.svms.SVMSControllerApplication]; nested exception is java.io.FileNotFoundException: class path resource [SVMSController.properties] cannot be opened because it does not exist
Dockerfile
FROM my.private.repo:5000/windows-java/8-jdk-windowsnanoserver-10.0.14393.1066:latest as builder WORKDIR application ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} application.jar RUN powershell java -Djarmode=layertools -jar application.jar extract FROM my.private.repo:5000/windows-java/8-jdk-windowsnanoserver-10.0.14393.1066:latest WORKDIR application COPY --from=builder application/dependencies/ ./ COPY --from=builder application/spring-boot-loader/ ./ COPY --from=builder application/snapshot-dependencies/ ./ COPY --from=builder application/application/ ./ EXPOSE 8080 ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher", "--spring.config.location=file:///C:/application/BOOT-INF/classes/config/SVMSController.properties"]
应用代码
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) @PropertySource("classpath:SVMSController.properties") public class SVMSControllerApplication implements ApplicationRunner { public static void main(String[] args) { SpringApplication.run(SVMSControllerApplication.class); } }
问题原因
- @PropertySource路径不匹配:
@PropertySource("classpath:SVMSController.properties")期望文件在类路径根目录(即BOOT-INF/classes/下),但实际挂载的文件位于BOOT-INF/classes/config/子目录,类路径下找不到根目录的该文件。 - 配置加载方式独立:
--spring.config.location是Spring Boot默认配置加载机制的扩展,而@PropertySource是显式指定的配置源,两者不会自动关联,指定spring.config.location不会让@PropertySource自动读取该路径的文件。
解决方案
方案一:修改@PropertySource路径
调整注解中的路径,指向配置文件实际的类路径位置:
@PropertySource("classpath:config/SVMSController.properties")
容器内C:/application/BOOT-INF/classes/是类路径根,config/SVMSController.properties对应实际文件位置,Spring可正常加载。
方案二:移除@PropertySource,依赖spring.config.location加载
删除代码中的@PropertySource注解,让Spring Boot通过指定的spring.config.location加载配置。同时调整Windows容器的路径格式(可简化file:///为file:):
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher", "--spring.config.location=file:C:/application/BOOT-INF/classes/config/SVMSController.properties"]
方案三:调整挂载路径到类路径根
将主机目录直接挂载到容器的类路径根目录,让文件处于BOOT-INF/classes/下,匹配@PropertySource的路径要求:
docker run -v C:\my-app-props:C:/application/BOOT-INF/classes/ <镜像名>
内容的提问来源于stack exchange,提问作者Alex998
相关产品推荐
相关产品推荐

