Docker容器化Spring Boot应用启动报错:MissingResourceException
解决Spring Boot部署到Docker Tomcat时的MissingResourceException异常
问题分析
报错java.util.MissingResourceException: Can't find bundle for base name application, locale en_US表明程序尝试加载名为application的资源包失败,结合你的配置,核心原因集中在以下几点:
@PropertySource默认不支持YAML格式,但你指向的是.yml配置文件,导致配置加载异常- 若Spring Security配置中使用了
ResourceBundleMessageSource且指定basename为application,会找不到对应资源文件 - Tomcat参数与Spring Boot环境变量的配置存在冲突,导致配置加载路径混乱
解决方案
1. 修复@PropertySource的YAML解析支持
@PropertySource默认仅能解析.properties文件,需自定义工厂类支持YAML:
修改ApplicationConfig注解:
@Configuration @PropertySource(value={"file:${application.properties}"}, factory = YamlPropertySourceFactory.class) @EnableJpaAuditing @EnableWebMvc @Import({ ConfigurationFiltreSecurite.class }) public class ApplicationConfig implements WebMvcConfigurer { }
添加YAML解析工厂类:
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.PropertySourceFactory; import java.io.IOException; import java.util.Properties; public class YamlPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(resource.getResource()); Properties properties = factory.getObject(); return new PropertiesPropertySource(resource.getResource().getFilename(), properties); } }
2. 修正资源包加载配置
检查ConfigurationFiltreSecurite中的消息源配置,确保指向正确的文件:
- 若使用
ResourceBundleMessageSource,避免将basename设为application,改为实际的消息文件前缀(如messages);若需加载外部YAML配置,改用ReloadableResourceBundleMessageSource:
@Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("file:/app/ml/awd/config/application-app"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; }
- 确保容器内
/app/ml/awd/config/目录下存在对应配置文件(若为消息资源,需是.properties格式,YAML需额外处理)。
3. 简化配置,消除冲突
移除Tomcat和@PropertySource的手动配置,改用Spring Boot原生环境变量加载配置:
- 删除
context-app.xml中的<Parameter>配置项 - 修改Docker Compose环境变量:
version: '3' services: tomcat: build: context: . dockerfile: Dockerfile.tomcat ports: - 8080:8080 depends_on: - mysql environment: - "SPRING_PROFILES_ACTIVE=dev" - "SPRING_CONFIG_LOCATION=file:/app/ml/awd/config/application-app.yml" volumes: - ./your-local-config-path:/app/ml/awd/config/ # 挂载本地配置到容器
- 移除
ApplicationConfig中的@PropertySource注解,让Spring Boot自动加载指定路径的配置。
4. 验证文件挂载
确保Dockerfile或Docker Compose的volumes配置正确,将本地配置文件挂载到容器内/app/ml/awd/config/目录,避免因文件缺失导致加载失败。
内容的提问来源于stack exchange,提问作者Ares
相关产品推荐
相关产品推荐

