异常解决 java.io.FileNotFoundException: class path resource [spring/applicationContext_*.xml]

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/hanxueyu666/article/details/72919406

maven创建web工程的时候报下面的错误

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:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.io.FileNotFoundException: class path resource [spring/] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:412)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:286)
at org.springframework.context.support.AbstractApplicationContext.getResources(AbstractApplicationContext.java:1159)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
... 19 more


查阅资料有人提出如下原因(http://blog.csdn.net/moneyshi/article/details/53287036)

一、问题的原因

为何会出现这种FIleNotFoundException呢?  原因无非两点:

1、路径配置错误 

2、编译有问题,文件不存在


二、解决办法:

对于第一个原因,这个我们不多说,大家自己看看Java Web classpath详解,改好配置目录路径,第一个原因排除,给大家一个案例参考




至于第二个原因,编译不成功,导致文件不存在,拿Eclipse or MyEclipse为例,  首先去项目的编译目录查看资源文件和class文件是否编译成功


如果编译目录的下不存在资源文件,那么编译不成功, 解决办法也很简单,选择Eclipse 工具栏上的Project , 选择clean up ,并勾选下面的 Build Automatically,这样就会自动编译了,等编译完成,你再看文件目录,是否存在资源文件,存在了,则完美解决


如果还不行,那么看看你是否是通过第三方插件或工具来进行编译的,重新对项目进行编译就好了


通过对比发现我是第二种原因,但用上述的方法不能解决后来想到了,是下面的原因造成的,要在工程的pom中,添加如下的

<build>
<resources>
            <resource>
                <directory>src/main/resource</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
</build>

如果不添加上面的语句,maven在编译的时候就会打.xml和.properties最为后缀的文件过滤掉,所以就会报错了

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页