解决:Could not resolve bean definition resource pattern [/WEB-INF/classes/spring/applicationContext-*.

Posted bbeb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决:Could not resolve bean definition resource pattern [/WEB-INF/classes/spring/applicationContext-*.相关的知识,希望对你有一定的参考价值。

问题:


用Maven搭建spring、springmvc、mybatis时,运行报错:

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

意思是说:
无法找到applicationContext-*.xml这个配置文件,因为这些文件不存在

原因:
在我的工程中,src/main/java下的mapper包中有mapper.java和mapper.xml文件,src/main/config目录有spring,mybatis,springmvc的配置文件
技术图片

 


这两个路径下最终对应maven的运行路径时:

技术图片

 


我们知道,maven在扫描java文件夹时,不会扫描其中的.xml文件,因为它默认是扫描java文件的,这样mapper.xml就会丢失而导致报错,所以我们会在pom文件中添加这样的配置:

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

上述配置的意思是:maven扫描src/main/java这个文件夹,并且要扫描所有.xml和.properties文件,这样一来可以解决maven扫描mapper.xml缺失的问题,但是由于修改了默认的resource目录,导致src/main/resources的所有文件都不能被扫描,也就出现了applicationContext文件不能被扫描的错误,所以应该配置两个:

<build> 
    <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
       由于修改了默认的resource目录,导致src/main/resources的所有文件都不能被扫描,因此还要配多一个
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

 

转自:https://blog.csdn.net/jeffleo/article/details/55271858

以上是关于解决:Could not resolve bean definition resource pattern [/WEB-INF/classes/spring/applicationContext-*.的主要内容,如果未能解决你的问题,请参考以下文章

org.springframework.beans.factory.annotation.value 防止不存在报错Could not resolve placeholder

org.springframework.beans.factory.annotation.value 防止不存在报错Could not resolve placeholder

Could not resolve placeholder 'jdbc username' in string valu

IDEA用github提交Could not resolve host: github.com的解决方案

ssh: Could not resolve hostname问题终于解决了?

centos解决Could not resolve host: mirrorlist.centos.org; Unknown error问题