首页 > 代码库 > Spring和Mybatis整合过程中遇到的一个找不到sqlSessionFactory或sqlSessionTemplate的异常

Spring和Mybatis整合过程中遇到的一个找不到sqlSessionFactory或sqlSessionTemplate的异常

先看启动web项目时IDEA控制台抛出的异常(红色部分):

D:\tomcat-kafka-8080\bin\catalina.bat run
[2017-04-16 03:14:47,718] Artifact Gradle : com.xbs:imcc : imcc-1.0-SNAPSHOT.war (exploded): Server is not connected. Deploy is not available.
Using CATALINA_BASE:   "C:\Users\SYJ\.IntelliJIdea14\system\tomcat\Unnamed_imcc_4"
Using CATALINA_HOME:   "D:\tomcat-kafka-8080"
Using CATALINA_TMPDIR: "D:\tomcat-kafka-8080\temp"
Using JRE_HOME:        "D:\java\jdk1.8.0_121"
Using CLASSPATH:       "D:\tomcat-kafka-8080\bin\bootstrap.jar;D:\tomcat-kafka-8080\bin\tomcat-juli.jar"
四月 16, 2017 3:14:48 下午 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\java\jdk1.8.0_121\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\java\scala\bin;D:\java\gradle-3.3\bin;D:\java\spark-1.6.2-bin-hadoop2.6\bin;D:\hadoop-2.6.4\bin;D:\apache-maven-3.2.1\bin;D:\apache-cxf-3.1.6\apache-cxf-3.1.6\bin;D:\java\jdk1.8.0_121\bin;%CATALINA_HOME%\bin;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Lenovo\Fingerprint Manager Pro\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;D:\java\mysql5.5\bin;D:\java\scala\bin;C:\Program Files\MySQL\MySQL Utilities 1.6\;F:\SVN\VisualSVNServer\bin;D:\java\TortoiseGit\bin;D:\java\TortoiseSVN-1.8.4\bin;D:\soft\ssh;.
四月 16, 2017 3:14:48 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
四月 16, 2017 3:14:48 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
四月 16, 2017 3:14:48 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
四月 16, 2017 3:14:48 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 670 ms
四月 16, 2017 3:14:48 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
四月 16, 2017 3:14:49 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.53
四月 16, 2017 3:14:49 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
四月 16, 2017 3:14:49 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
四月 16, 2017 3:14:49 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 55 ms
Connected to server
[2017-04-16 03:14:49,321] Artifact Gradle : com.xbs:imcc : imcc-1.0-SNAPSHOT.war (exploded): Artifact is being deployed, please wait...
四月 16, 2017 3:14:49 下午 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\lib\javax.servlet-api-3.0.1.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
四月 16, 2017 3:14:49 下午 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\lib\servlet-api-2.4.jar) - jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/core_rt is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/core is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/core is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/fmt_rt is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/fmt is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/fmt is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/functions is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/permittedTaglibs is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://jakarta.apache.org/taglibs/standard/scriptfree is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/sql_rt is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/sql is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/sql is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/xml_rt is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jstl/xml is already defined
四月 16, 2017 3:14:53 下午 org.apache.catalina.startup.TaglibUriRule body
信息: TLD skipped. URI: http://java.sun.com/jsp/jstl/xml is already defined
log4j:WARN No such property [conversionPattern] in net.logstash.log4j.JSONEventLayoutV1.
2017-04-16 15:14:53,420  INFO ContextLoader:285 - Root WebApplicationContext: initialization started
2017-04-16 15:14:53,559  INFO XmlWebApplicationContext:510 - Refreshing Root WebApplicationContext: startup date [Sun Apr 16 15:14:53 CST 2017]; root of context hierarchy
2017-04-16 15:14:53,615  INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [spring/applicationContext.xml]
2017-04-16 15:14:54,902  INFO PropertySourcesPlaceholderConfigurer:172 - Loading properties file from file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\resource\db.properties]
2017-04-16 15:14:54,903  INFO PropertySourcesPlaceholderConfigurer:172 - Loading properties file from file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\resource\resource.properties]
2017-04-16 15:14:54,915  INFO AutowiredAnnotationBeanPostProcessor:153 - JSR-330 javax.inject.Inject annotation found and supported for autowiring
2017-04-16 15:14:55,043  WARN XmlWebApplicationContext:487 - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name personController: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.service.PersonService com.xbs.api.PersonController.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personServiceImpl: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.dao.PersonDao com.xbs.service.impl.PersonServiceImpl.personDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1740)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.service.PersonService com.xbs.api.PersonController.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personServiceImpl: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.dao.PersonDao com.xbs.service.impl.PersonServiceImpl.personDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 58 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name personServiceImpl: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.dao.PersonDao com.xbs.service.impl.PersonServiceImpl.personDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 60 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.dao.PersonDao com.xbs.service.impl.PersonServiceImpl.personDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 71 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 73 more
Caused by: java.lang.IllegalArgumentException: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:75)
    at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 83 more
2017-04-16 15:14:55,068 ERROR ContextLoader:331 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name personController: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.service.PersonService com.xbs.api.PersonController.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personServiceImpl: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.dao.PersonDao com.xbs.service.impl.PersonServiceImpl.personDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
    at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1740)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
    at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
    at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
    at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
    at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
    at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
    at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
    at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.service.PersonService com.xbs.api.PersonController.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personServiceImpl: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.dao.PersonDao com.xbs.service.impl.PersonServiceImpl.personDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 58 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name personServiceImpl: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.dao.PersonDao com.xbs.service.impl.PersonServiceImpl.personDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 60 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.xbs.dao.PersonDao com.xbs.service.impl.PersonServiceImpl.personDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 71 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name personDaoImpl defined in file [E:\imcc\out\artifacts\imcc\exploded\imcc-1.0-SNAPSHOT.war\WEB-INF\classes\com\xbs\dao\impl\PersonDaoImpl.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property sqlSessionFactory or sqlSessionTemplate are required
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 73 more
Caused by: java.lang.IllegalArgumentException: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘ are required
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:75)
    at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 83 more
log4j:WARN No such property [conversionPattern] in net.logstash.log4j.JSONEventLayoutV1.
四月 16, 2017 3:14:55 下午 org.apache.catalina.core.StandardContext startInternal
严重: Error listenerStart
四月 16, 2017 3:14:55 下午 org.apache.catalina.core.StandardContext startInternal
严重: Context [] startup failed due to previous errors
[2017-04-16 03:14:55,181] Artifact Gradle : com.xbs:imcc : imcc-1.0-SNAPSHOT.war (exploded): Error during artifact deployment. See server log for details.

意思是说没有找到sqlSessionFactory或者sqlSessionTemplate属性。为什么呢?

我的项目使用的mybatis的版本如下(gradle):

compile "org.mybatis:mybatis:3.3.0"
compile "org.mybatis:mybatis-spring:1.2.3"

在项目中的Dao层是这样使用使用SQLSession的:

package com.xbs.dao.impl;

import java.util.List;
import java.util.Map;

import com.xbs.dao.PersonDao;
import com.xbs.model.Person;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

@Repository
public class PersonDaoImpl extends SqlSessionDaoSupport implements PersonDao {

    String ns = "sql.mapper.PersonMapper.";
    @Override
    public void save(Person p) {
        this.getSqlSession().insert(ns+"insert", p);
    }

    @Override
    public Person selectPersonById(Integer personId) {
        return (Person) this.getSqlSession().selectOne(ns+"selectPersonByIdWithRM", personId);
    }

    @Override
    public void update(Person p) {
        this.getSqlSession().update(ns+"dynamicUpdate", p);
    }

    @Override
    public void delete(Integer personId) {
        this.getSqlSession().delete(ns+"delete", personId);
    }

    @Override
    public List<Person> selectPersonByCondition(Map<String, Object> map) {
        return this.getSqlSession().selectList(ns+"selectPersonByCondition", map);
    }

}

Dao层继承了SqlSessionDaoSupport,该类会自动注入SQLSessionFectory,我们只要使用this.getSqlSession()就可以拿到SqlSession。

但是如果使用的是"mybatis-spring-1.0.0-RC3.jar"这个版本是没有问题的。但是高版本就会有问题,原因是Mybatis3依赖的jar包"mybatis-spring-1.2.0.jar"这个版本以及以上的版本中,对SqlSessionDaoSupport类中的‘sqlSessionFactory‘或‘sqlSessionTemplate‘注入方式进行了调整。

可能是为了解决多数据源的问题吧,取消了自动注入。

解决办法:创建一个Dao的基类CommonDao,让这个基类继承SqlSessionDaoSupport,并通过set方法注入SqlSessionFactory属性即可:

/**
 * Created by SYJ on 2017/4/16.
 */
public class CommonDao extends SqlSessionDaoSupport {
    @Resource
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
        super.setSqlSessionFactory(sqlSessionFactory);
    }
}

然后让Dao实现类再继承这个CommonDao基类即可:

@Repository
public class PersonDaoImpl extends CommonDao implements PersonDao {
  //此处省略...  
}

重新启动web项目,问题解决。

 

Spring和Mybatis整合过程中遇到的一个找不到sqlSessionFactory或sqlSessionTemplate的异常