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

problem:


When setting up the spring, springmvc, mybatis with Maven, run error:

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

This means that:
can not find applicationContext - * xml configuration file, because the file does not exist.

The reason:
In my project, mapper package under src / main / java has mapper.java and mapper.xml file, src / main / config directory has spring, mybatis, springmvc profile

 


When these two paths corresponding to the final running path of the maven:

 


We know that, maven when scanning java folder, which does not scan .xml file, because it is the default scanning java files so mapper.xml will lead to the loss of error, so we'll add this in the pom file configuration:

<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

Guess you like

Origin www.cnblogs.com/bbeb/p/11069789.html