本文主要是介绍java.io.FileNotFoundException: Could not open ServletContext resource [/config.properties],希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
错误
SpringBoot项目,单元测试的时候,报错:
java.io.FileNotFoundException: Could not open ServletContext resource [/config.properties]
测试代码:
@Testpublic 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,问题解决。
参考:
【1】:springboot juint 测试 Could not open ServletContext resource [/application.yml]
这篇关于java.io.FileNotFoundException: Could not open ServletContext resource [/config.properties]的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!