class path resource [spring/] cannot be resolved to URL because it does not exist

使用Maven 经常会出现一些问题。今天项目出了点 问题,在多次尝试下无果,然后在 Maven 里就 clean 了项目,然后重新编译,这时报错如下。

[CONSOLE] 2017-09-18 09:19:54,764 - org.springframework.web.context.ContextLoader -1 [RMI TCP Connection(2)-127.0.0.1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
 org.springframework.beans.factory.BeanDefinitionStoreException: Could not resolve bean definition resource pattern [classpath:spring/applicationContext-*.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/] cannot be resolved to URL because it does not exist
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:229)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
 at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
 at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
 at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
 at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:604)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:509)
 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
 at

主要是这句

Could not resolve bean definition resource pattern [classpath:spring/applicationContext-*.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring/] cannot be resolved to URL because it does not exist

很明显是 无法找到 classpath 下的 spring 文件夹。

这个问题和 Maven 无法生成resource下的文件 相同。

先看 项目结构

class path resource [spring/] cannot be resolved to URL because it does not exist

 

然后 我们看了一下 Maven 编译生成的文件,md,怎么只有一个log4j的配置文件,我的spring和mybatis 文件夹呢?

class path resource [spring/] cannot be resolved to URL because it does not exist

 

我记得在 pom.xml 里已经配置过了让其生成,然后网上查了一些人家的方法,纳尼?怎么这里是 true ?我的是 false 。

<filtering>true</filtering>

最终解决方案如下

在 pom.xml 里添加如下代码

  1.  <build>
  2.     <finalName>ForestBlog</finalName>
  3.     <resources>
  4.       <resource>
  5.         <directory>src/main/java</directory>
  6.         <includes>
  7.           <include>**/*.properties</include>
  8.           <include>**/*.xml</include>
  9.         </includes>
  10.         <filtering>true</filtering>
  11.       </resource>
  12.     </resources>
  13. </build>

我的错误是 第10行 是 false,那为什么之前却有用呢?难不成谁给我改成false了?

然后再次启动 Tomcat,就可以看到Maven在 target 生成了文件

class path resource [spring/] cannot be resolved to URL because it does not exist

 

如果还是不行,我还是会遇到添加了上面的代码,但是还是无法生成 mybatis和spring,以及那两个properties文件的情况。不要急,这时候可以试试 clean (删除target),然后在启动 Tomcat。

 

 

本文地址:https://liuyanzhao.com/6255.html

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
言曌

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: