java.io.FileNotFoundException: Could not open ServletContext resource [/config.properties]

简介: java.io.FileNotFoundException: Could not open ServletContext resource [/config.properties]

错误

SpringBoot项目,单元测试的时候,报错:

 java.io.FileNotFoundException: Could not open ServletContext resource [/config.properties]

测试代码:

    @Test
    public void  testGetCreditListByPost(){
        String result=creditService.getCreditListByPost("XX公司");
        log.info(result);
    }

完整报错:

Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/config.properties]
  at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:159) ~[spring-web-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:159) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:99) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:73) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.core.io.support.PropertiesLoaderUtils.loadProperties(PropertiesLoaderUtils.java:59) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.core.io.support.ResourcePropertySource.<init>(ResourcePropertySource.java:67) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.core.io.support.DefaultPropertySourceFactory.createPropertySource(DefaultPropertySourceFactory.java:37) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:453) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:272) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:194) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:202) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:170) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
  ... 36 common frames omitted

原因

这个原因不是config.properties配置找不到,而是项目中存在@PropertySource注解 。

@PropertySource("config.properties")

解决办法

@PropertySource("config.properties")

替换成:

@PropertySource("classpath:config.properties")

OK,问题解决。


目录
相关文章
|
6月前
|
Java
Java中的@Resource和@Autowire有哪些区别?
Java中的@Resource和@Autowire有哪些区别?
129 0
|
JSON Java 数据格式
Java读取resource目录下的json文件
Java读取resource目录下的json文件
613 0
|
6月前
|
数据安全/隐私保护
java.nio.file.AccessDeniedException: /home/soft/elasticsearch-6.8.0/config/jvm.options
java.nio.file.AccessDeniedException: /home/soft/elasticsearch-6.8.0/config/jvm.options
68 0
|
6月前
|
Java
java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/YamlProcessor$StrictMapAppe
java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/YamlProcessor$StrictMapAppe
242 0
|
3月前
|
JavaScript Java Python
【Azure 应用服务】在Azure App Service for Windows 中部署Java/NodeJS/Python项目时,web.config的配置模板内容
【Azure 应用服务】在Azure App Service for Windows 中部署Java/NodeJS/Python项目时,web.config的配置模板内容
|
6月前
|
XML Java Maven
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
176 0
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
|
5月前
|
存储 消息中间件 Java
Java一分钟之-Spring Cloud Config:外部化配置
【6月更文挑战第8天】Spring Cloud Config提供外部化配置,通过Config Server管理和版本控制微服务配置。本文涵盖Config Server与Client的配置、常见错误、多环境配置、实时更新及使用示例。注意配置服务器URL、环境变量设置、Bus配置以及安全问题。使用Config能提升系统灵活性和可维护性,但要留意日志以确保配置正确和安全。
146 10
|
6月前
|
安全 Java 数据库连接
java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/YamlProcessor$StrictMapAppe
java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/YamlProcessor$StrictMapAppe
57 0
|
6月前
|
XML 前端开发 Java
SpringMVC中那些Java Config
SpringMVC中那些Java Config
101 0
|
3月前
|
存储 Java
【IO面试题 四】、介绍一下Java的序列化与反序列化
Java的序列化与反序列化允许对象通过实现Serializable接口转换成字节序列并存储或传输,之后可以通过ObjectInputStream和ObjectOutputStream的方法将这些字节序列恢复成对象。

热门文章

最新文章

下一篇
无影云桌面