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