To manage Spring+mybatis projects with Maven:
This is mainly about the configuration of the Pom.xml file in Maven, and a few issues that will come up during the MAVEN build process (I'm using the maven4.4 version):
The first step is to configure the dependencies required for the project: For ease of administration, it is best to write the version number and dependencies separately:
The following configuration methods are as follows:
<properties><!--Spring version number--><spring.version>3.2.4. Release</spring.version> <!--mybatis version number--><mybatis.version>3.2.4</mybatis.version><! --log4j Log File Management Pack version--><slf4j.version>1.6.6</slf4j.version><log4j.version>1.2.9</ log4j.version><!--mysql version--><mysql.version>5.1.29</mysql.version><!--Google JSON version-- ><gson.version>2.5</gson.version><!--JUnit Version--><junit.version>4.11</ junit.version><!--Com.google.guava version--><guava.version>19.0</guava.version><!-- Commons-codec version--><commons-codec.version>1.10</commons-codec.version> </properties> < Dependency><groupid>org.springframework</groupid><artifactid>spring-core</artifactid ><version>${spring.version}</version></dependency>
There is nothing to say about the configuration of the jar dependencies, but here are a few of the issues that arise during the package process:
1: The project's spring and mybatis XML files are not found in the target directory after packaging;
By default, when Maven is packaged, only the. java files are compiled, and the Spring.xml file manages the dependencies of all the classes, so this error is not found for the corresponding class;
Severity: Exception sending context initialized event to listener instance of class Org.springframework.web.context.ContextLoad ErListenerorg.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' Usercontroller ': Injection of resource dependencies failed; Nested exception is org.springframework.beans.factory.BeanCreationException:Error creating beans with Name ' UserService ': Injection of resource dependencies failed; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [ Bz.sunlight.dao.UserDAO] found for dependency:expected at least 1 beans which qualifies as Autowire candidate for this DEP Endency. Dependency annotations: {@javax. Annotation.resource (Shareable=true, Mappedname=, description=, Name=, Type=class Java.lang.Object, Authenticationtype=container, lookup=)}related cause: Org.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' Userdao ' defined in file [d:\ DmcworkspaCe\maventest\target\classes\bz\sunlight\dao\userdao.class]: Cannot resolve reference to bean ' sqlsessionfactory ' While setting beans property ' sqlsessionfactory '; Nested exception is org.springframework.beans.factory.BeanCreationException:Error creating beans with Name ' Sqlsessionfactory ' defined in class path resource [Spring-mybatis.xml]: Invocation of Init method failed; Nested exception is org.springframework.core.NestedIOException:Failed to parse mapping resource: ' Class path resource [BZ /sunlight/daomapping/userdao.xml] '; Nested exception is java.io.FileNotFoundException:class path resource [Bz/sunlight/daomapping/userdao.xml] cannot be Opened because it does not existat Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues ( commonannotationbeanpostprocessor.java:306) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean ( abstractautowirecapablebeanfactory.java:1116) at Org.springframework.beans.factOry.support.AbstractAutowireCapableBeanFactory.doCreateBean (abstractautowirecapablebeanfactory.java:519) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:458) at org.springframework.beans.factory.support.abstractbeanfactory$1. GetObject (abstractbeanfactory.java:295) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:223) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:292) at Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:194) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons ( defaultlistablebeanfactory.java:628) at Org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization ( abstractapplicationcontext.java:932) at Org.springframework.context.support.AbstractApplicatioNcontext.refresh (abstractapplicationcontext.java:479) at Org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext (contextloader.java:389) At Org.springframework.web.context.ContextLoader.initWebApplicationContext (contextloader.java:294) at Org.springframework.web.context.ContextLoaderListener.contextInitialized (contextloaderlistener.java:112) 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.run (futuretask.java:262) at Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145) at Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615) atJava.lang.Thread.run (thread.java:745) caused by:org.springframework.beans.factory.BeanCreationException:Error Creating bean with Name ' UserService ': Injection of resource dependencies failed; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [ Bz.sunlight.dao.UserDAO] found for dependency:expected at least 1 beans which qualifies as Autowire candidate for this DEP Endency. Dependency annotations: {@javax. Annotation.resource (Shareable=true, Mappedname=, description=, Name=, Type=class Java.lang.Object, Authenticationtype=container, lookup=)}at Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues ( commonannotationbeanpostprocessor.java:306) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean ( abstractautowirecapablebeanfactory.java:1116) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (Abstractautowirecapablebeanfactory.java:519) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:458) at org.springframework.beans.factory.support.abstractbeanfactory$1. GetObject (abstractbeanfactory.java:295) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:223) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:292) at Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:198) at Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource ( commonannotationbeanpostprocessor.java:442) at Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource ( commonannotationbeanpostprocessor.java:416) at Org.springframework.context.annotation.commonannotationbeanpostprocessor$resourceelement.getresourcetoinject ( Commonannotationbeanpostprocessor.java:550) at Org.springframework.beans.factory.annotation.injectionmetadata$injectedelement.inject ( injectionmetadata.java:150) at Org.springframework.beans.factory.annotation.InjectionMetadata.inject ( injectionmetadata.java:87) at Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues ( commonannotationbeanpostprocessor.java:303) ... More
Of course, not only the spring and mybatis XML files in the project, but also the SQL mapping file, the following configuration should be added under the Pom file, so that the XML file can be packaged when MAVEN is packaged:
<resource> <directory>src/main/resources</directory> <includes> < include>**/*.xml</include> </includes> <filtering>true</filtering> < /resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering > </resource>
This can be packaged in the package,src/main/resources and src/main/java XML files are packaged;
2: Question 2: Byte 3 of the UTF-8 sequence of the Malformedbytesequenceexception:3 byte is invalid;
If you do not configure the encoding plug-in, the package will be packaged this error, just under the Pom.xml file configuration as follows:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId> maven-resources-plugin</artifactid> <configuration> <encoding>UTF-8</encoding> </configuration>
These two issues are MAVEN packaging problems encountered, but also troubled me for a long time, finally solved;
Severity: Exception sending context initialized event to listener instance of class Org.springframework.web.context.ContextLoad ErListenerorg.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' Usercontroller ': Injection of resource dependencies failed; Nested exception is org.springframework.beans.factory.BeanCreationException:Error creating beans with Name ' UserService ': Injection of resource dependencies failed; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [ Bz.sunlight.dao.UserDAO] found for dependency:expected at least 1 beans which qualifies as Autowire candidate for this DEP Endency. Dependency annotations: {@javax. Annotation.resource (Shareable=true, Mappedname=, description=, Name=, Type=class Java.lang.Object, Authenticationtype=container, lookup=)}related cause: Org.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' Userdao ' defined in file [d:\ DmcworkspaCe\maventest\target\classes\bz\sunlight\dao\userdao.class]: Cannot resolve reference to bean ' sqlsessionfactory ' While setting beans property ' sqlsessionfactory '; Nested exception is org.springframework.beans.factory.BeanCreationException:Error creating beans with Name ' Sqlsessionfactory ' defined in class path resource [Spring-mybatis.xml]: Invocation of Init method failed; Nested exception is org.springframework.core.NestedIOException:Failed to parse mapping resource: ' Class path resource [BZ /sunlight/daomapping/userdao.xml] '; Nested exception is java.io.FileNotFoundException:class path resource [Bz/sunlight/daomapping/userdao.xml] cannot be Opened because it does not existat Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues ( commonannotationbeanpostprocessor.java:306) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean ( abstractautowirecapablebeanfactory.java:1116) at Org.springframework.beans.factOry.support.AbstractAutowireCapableBeanFactory.doCreateBean (abstractautowirecapablebeanfactory.java:519) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:458) at org.springframework.beans.factory.support.abstractbeanfactory$1. GetObject (abstractbeanfactory.java:295) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:223) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:292) at Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:194) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons ( defaultlistablebeanfactory.java:628) at Org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization ( abstractapplicationcontext.java:932) at Org.springframework.context.support.AbstractApplicatioNcontext.refresh (abstractapplicationcontext.java:479) at Org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext (contextloader.java:389) At Org.springframework.web.context.ContextLoader.initWebApplicationContext (contextloader.java:294) at Org.springframework.web.context.ContextLoaderListener.contextInitialized (contextloaderlistener.java:112) 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.run (futuretask.java:262) at Java.util.concurrent.ThreadPoolExecutor.runWorker (threadpoolexecutor.java:1145) at Java.util.concurrent.threadpoolexecutor$worker.run (threadpoolexecutor.java:615) atJava.lang.Thread.run (thread.java:745) caused by:org.springframework.beans.factory.BeanCreationException:Error Creating bean with Name ' UserService ': Injection of resource dependencies failed; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [ Bz.sunlight.dao.UserDAO] found for dependency:expected at least 1 beans which qualifies as Autowire candidate for this DEP Endency. Dependency annotations: {@javax. Annotation.resource (Shareable=true, Mappedname=, description=, Name=, Type=class Java.lang.Object, Authenticationtype=container, lookup=)}at Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues ( commonannotationbeanpostprocessor.java:306) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean ( abstractautowirecapablebeanfactory.java:1116) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (Abstractautowirecapablebeanfactory.java:519) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:458) at org.springframework.beans.factory.support.abstractbeanfactory$1. GetObject (abstractbeanfactory.java:295) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:223) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:292) at Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:198) at Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource ( commonannotationbeanpostprocessor.java:442) at Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource ( commonannotationbeanpostprocessor.java:416) at Org.springframework.context.annotation.commonannotationbeanpostprocessor$resourceelement.getresourcetoinject ( Commonannotationbeanpostprocessor.java:550) at Org.springframework.beans.factory.annotation.injectionmetadata$injectedelement.inject ( injectionmetadata.java:150) at Org.springframework.beans.factory.annotation.InjectionMetadata.inject ( injectionmetadata.java:87) at Org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues ( commonannotationbeanpostprocessor.java:303) ... More
Spring3.2+mybatis3.2+maven Consolidation