Nested exception is java.io.FileNotFoundException:class path resource [Spring-mybatis.xml] cannot be opened

Source: Internet
Author: User

IDEA+MAVEN+SPRINGMVC project, do unit test error, unable to find configuration file.

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= {"Classpath:spring-mybatis.xml"}) Public classTestmybatis {Private StaticLogger Logger = Loggerfactory.getlogger (testmybatis.class); @ResourcePrivateMessageservice messageservice=NULL; @Test Public  voidtest1 () {Message message=messageservice.getmessagebyid (1); Logger.info ("Command====:{}", Message.getcommand ()); }}

Click "Classpath:spring-mybatis.xml" to find the file.

The configuration file is placed in the Src/main/resouces directory and is set to resources Root. However, it does not work, and the configuration file is not found under the Target/classes file after compilation.

After searching online, there are two ways to solve this problem.

The first is to create a src/main/resources folder and place the resource files such as XML in this directory. The Maven tool is compiled by default, and the resource files in the Resources folder are packaged into the classes directory.

The second solution is to configure MAVEN's Pom file configuration, locate the <build> node in the Pom file, and add the following code:

<Build>        <Resources>            <Resource>                <Directory>Src/main/java</Directory>                <includes>                    <include>**/*.xml</include>                </includes>            </Resource>        </Resources>    </Build>

Obviously, the first one is not for me in this way.

I looked at the next pom.xml, because it is a project to learn, copy on-line. Found that there is already a resource configuration, which is posted above. (Because Src/main/java has mapper.xml below, so it is configured).

So I deleted <resources> node, recompile, found src/main/resouces the following configuration file has been to the Target/classes folder. But unfortunately, the previous mapper.xml is not under the classes file.

But it doesn't matter, we put both of them in the resources.

<Build>    <Resources>         <Resource>            <Directory>Src/main/java</Directory>            <includes>                    <include>**/*.xml</include>             </includes>            </Resource>            <Resource>                <Directory>Src/main/resources</Directory>          </Resource>     </Resources></Build>

Nested exception is java.io.FileNotFoundException:class path resource [Spring-mybatis.xml] cannot be opened

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.