欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring+springmvc整合mabytis时mapper注入失败问题解决方法

程序员文章站 2024-02-23 21:14:40
好久不搭架构,今天使用spring+springmvc整合mabytis的时候,坑爹的两个问题发生了。 花了一晚上解决这两个问题。现在将错误经验总结分享下,避免以后遇到坑...

好久不搭架构,今天使用spring+springmvc整合mabytis的时候,坑爹的两个问题发生了。

花了一晚上解决这两个问题。现在将错误经验总结分享下,避免以后遇到坑继续往里跳。

1. 单元测试的时候,单元测试失败报错: a servletcontext is required to configure default servlet handling

十月 28, 2016 9:03:33 下午 org.springframework.context.support.genericapplicationcontext refresh
警告: exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.beancreationexception: error creating bean with name 'defaultservlethandlermapping' defined in class path resource [org/springframework/web/servlet/config/annotation/delegatingwebmvcconfiguration.class]: bean instantiation via factory method failed; nested exception is org.springframework.beans.beaninstantiationexception: failed to instantiate [org.springframework.web.servlet.handlermapping]: factory method 'defaultservlethandlermapping' threw exception; nested exception is java.lang.illegalargumentexception: a servletcontext is required to configure default servlet handling
  at org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:599)
  at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiateusingfactorymethod(abstractautowirecapablebeanfactory.java:1119)
  at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1014)
  at org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:504)
  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.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:125)
  at org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:60)
  at org.springframework.test.context.support.abstractdelegatingsmartcontextloader.delegateloading(abstractdelegatingsmartcontextloader.java:109)
  at org.springframework.test.context.support.abstractdelegatingsmartcontextloader.loadcontext(abstractdelegatingsmartcontextloader.java:261)
  at org.springframework.test.context.defaultcacheawarecontextloaderdelegate.loadcontextinternal(defaultcacheawarecontextloaderdelegate.java:68)
  at org.springframework.test.context.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:86)
  at org.springframework.test.context.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:72)
  at org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.injectdependencies(dependencyinjectiontestexecutionlistener.java:117)
  at org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.preparetestinstance(dependencyinjectiontestexecutionlistener.java:83)
  at org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:212)
  at org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:200)
  at org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:259)
  at org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12)
  at org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:261)
  at org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:219)
  at org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:83)
  at org.junit.runners.parentrunner$3.run(parentrunner.java:290)
  at org.junit.runners.parentrunner$1.schedule(parentrunner.java:71)
  at org.junit.runners.parentrunner.runchildren(parentrunner.java:288)
  at org.junit.runners.parentrunner.access$000(parentrunner.java:58)
  at org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268)
  at org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61)
  at org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:68)
  at org.junit.runners.parentrunner.run(parentrunner.java:363)
  at org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:163)
  at org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50)
  at org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:459)
  at org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675)
  at org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382)
  at org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192)
caused by: org.springframework.beans.beaninstantiationexception: failed to instantiate [org.springframework.web.servlet.handlermapping]: factory method 'defaultservlethandlermapping' threw exception; nested exception is java.lang.illegalargumentexception: a servletcontext is required to configure default servlet handling
  at org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:189)
  at org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:588)
  ... 42 more

web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>archetype created web application</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- 指定spring初始上下文环境-指定spring-core配置文件 -->
  <context-param>
    <param-name>contextconfiglocation</param-name>
    <param-value>classpath:spring_ynyn_dependence.xml</param-value>
  </context-param>

  <!-- 字符编码过滤器 -->
  <filter>
    <filter-name>characterencoding</filter-name>
    <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class>
    <init-param>
      <param-name>forceencoding</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterencoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!-- spring-mvc分发器配置并指定spring-mvc配置文件 -->
  <servlet>
    <servlet-name>dispatcherservlet</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.dispatcherservlet
    </servlet-class>
    <init-param>
      <param-name>contextconfiglocation</param-name>
      <param-value>
        classpath:spring-mvc.xml
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherservlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>hiddenhttpmethodfilter</filter-name>
    <filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>hiddenhttpmethodfilter</filter-name>
    <servlet-name>dispatcherservlet</servlet-name>
  </filter-mapping>
</web-app>

资源以及配置文件都放在src/main/respources目录下。

springmvc的配置文件spring-mvc.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
  xsi:schemalocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd  
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/util 
      http://www.springframework.org/schema/util/spring-util-3.1.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task.xsd">


  <context:component-scan base-package="org.byron4j.ynyn.*" >
    <context:include-filter type="annotation" expression="org.springframework.stereotype.controller" />
  </context:component-scan> 

  <!-- 默认的注解映射的支持 -->
  <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
      <bean class="org.springframework.http.converter.stringhttpmessageconverter">
        <constructor-arg value="utf-8" />
      </bean>
      <bean
        class="com.alibaba.fastjson.support.spring.fastjsonhttpmessageconverter">
        <property name="supportedmediatypes" value="application/json;charset=utf-8" />
        <property name="features">
          <array>
            <value>writemapnullvalue</value>
            <value>writenullstringasempty</value>
          </array>
        </property>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>

  <!-- 静态资源映射 -->
  <mvc:resources mapping="/js/**" location="/js/" />
  <mvc:resources mapping="/css/**" location="/css/" />
  <mvc:resources mapping="/images/**" location="/images/" />
  <!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 -->
  <mvc:default-servlet-handler />

  <!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
  <bean
    class="org.springframework.web.servlet.view.internalresourceviewresolver">
    <property name="viewclass"
      value="org.springframework.web.servlet.view.jstlview" />
    <property name="prefix" value="/web-inf/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
</beans>

sprin框架的配置文件spring_ynyn_dependence.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:task="http://www.springframework.org/schema/task"
  xsi:schemalocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/util 
      http://www.springframework.org/schema/util/spring-util-3.1.xsd
      http://www.springframework.org/schema/task 
      http://www.springframework.org/schema/task/spring-task.xsd">
  <util:properties id="valuesettings" location="classpath:globalconfig.properties" />

  <context:component-scan base-package="org.byron4j.ynyn.*" > 
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller" />
  </context:component-scan>


  <!--引入定时任务注解驱动,可以在应用中使用@scheduled注解开发定时任务-->
  <task:annotation-driven />

  <!--引入数据源配置,包括mybatis相关的配置-->
  <import resource="classpath*:byron4j_ynyn_datasource.xml"/>
</beans>

数据源配置文件byron4j_ynyn_datasource.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemalocation="
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.1.xsd
      http://www.springframework.org/schema/tx  
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

  <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
    <property name="configlocation" value="classpath:byron4j_ynyn_mybatis-config.xml" />
    <!--我的项目的表的mapper.xml文件放在src/main/resources目录下-->
    <property name="mapperlocations" value="classpath*:sqlmap/*.xml" />
    <property name="datasource" ref="datasource" />
  </bean>


  <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
    <property name="basepackage" value="org.byron4j.ynyn.mapper" />
    <property name="sqlsessionfactorybeanname" value="sqlsessionfactory"></property>
  </bean>

  <!-- 事务管理 -->
  <bean id="mytxmanager"
    class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
    <property name="datasource" ref="datasource"></property>
  </bean>

  <tx:annotation-driven transaction-manager="mytxmanager" />

  <!--这里使用的是sqlite数据库,是一种文件数据库,用户名密码的账户必须拥有可以读写ynyn.sqlite文件的权限-->
  <bean id="datasource" 
     class="org.apache.commons.dbcp.basicdatasource" 
     destroy-method="close"> 
    <property name="driverclassname" value="org.sqlite.jdbc"/> 
    <property name="url" value="jdbc:sqlite:e:\ynyn.sqlite"/> 
    <property name="username" value="username"/> 
    <property name="password" value="password"/> 
    <!--maxactive: 最大连接数量-->  
    <property name="maxactive" value="200"/> 
    <!--minidle: 最小空闲连接-->  
    <property name="minidle" value="10"/> 
    <!--maxidle: 最大空闲连接-->  
    <property name="maxidle" value="50"/>
    <!--initialsize: 初始化连接-->  
    <property name="initialsize" value="50"/> 
    <!-- 连接被泄露时是否打印 --> 
    <property name="logabandoned" value="true"/> 
    <!--removeabandoned: 是否自动回收超时连接-->  
    <property name="removeabandoned" value="true"/> 
    <!--removeabandonedtimeout: 超时时间(以秒数为单位)-->  
    <property name="removeabandonedtimeout" value="180"/>
    <!--maxwait: 超时等待时间以毫秒为单位 1000等于60秒--> 
    <property name="maxwait" value="1000"/> 
    <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. --> 
    <property name="timebetweenevictionrunsmillis" value="10000"/> 
    <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 --> 
    <property name="numtestsperevictionrun" value="10"/> 
    <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程--> 
    <property name="minevictableidletimemillis" value="10000"/> 
    <property name="validationquery" value="select 1 limit 1 "/> 
  </bean>

</beans>

mybatis配置文件byron4j_ynyn_mybatis-config.xml

<?xml version="1.0" encoding="utf-8"?>
  <!doctype configuration
    public "-//mybatis.org//dtd config 3.0//en"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
  <configuration>

</configuration>

单元测试基类basemockitotestcase.java

src/test/java目录下:

package org.byron4j.ynyn.base;

import org.junit.after;
import org.junit.before;
import org.junit.runner.runwith;
import org.mockito.mockitoannotations;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;

/**
 * mock测试类基类--配置文件载入类
 * 
 * @classname: basespringtestcase
 * 
 */
@runwith(springjunit4classrunner.class)
@contextconfiguration(locations = { "classpath:spring_ynyn_dependence.xml" })
public class basemockitotestcase {
  @before
  public void beforeinvoke() {
    system.out.println("调用前工作....");
    mockitoannotations.initmocks(this);
  }

  @after
  public void afterinvoke() {
    system.out.println("调用完成.");
  }

}

单元测试类studenttest.java出问题了

package org.byron4j.ynyn.dbtest;

import org.junit.test;
import org.springframework.beans.factory.annotation.autowired;

import org.byron4j.ynyn.base.basemockitotestcase;
import org.byron4j.ynyn.mapper.taskermapper;
import org.byron4j.ynyn.mode.tasker;



public class studenttest extends basemockitotestcase{

  @autowired
  studentmapper studentmapper;


  @test
  public void selecttest4success(){
    student record = new student();
    record.setid(1);
    student resultstudent = studentmapper.selectbyprimarykey(record);
    system.out.println(resultstudent);;
  }
}

执行单元测试的时候,报错了警告: exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.beancreationexception: error creating bean with name ‘defaultservlethandlermapping' defined in class path resource [org/springframework/web/servlet/config/annotation/delegatingwebmvcconfiguration.class]: bean instantiation via factory method failed; nested exception is org.springframework.beans.beaninstantiationexception: failed to instantiate [org.springframework.web.servlet.handlermapping]: factory method ‘defaultservlethandlermapping' threw exception; nested exception is java.lang.illegalargumentexception: a servletcontext is required to configure default servlet handling

这时候去检查spring-core和springmvc的两个配置文件,异常提示信息大意是servletcontext需要配置默认servlet处理映射器。一脸懵逼。。。最后发现是两个配置文件的context和包扫描配置有问题,springservlet上下文应该在spring核心配置中( spring_ynyn_dependence.xml )处理,需要使用 <context:annotation-config /> 进行配置。调整后的配置是:

1.sprin框架的配置文件spring_ynyn_dependence.xml调整

将以下配置内容
<context:component-scan base-package="org.byron4j.ynyn.*" > 
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller" />
  </context:component-scan>
替换为:
<context:annotation-config />

2.springmvc的配置文件spring-mvc.xml调整

<context:component-scan base-package="com.crfchina.mcp.*" >
    <context:include-filter type="annotation" expression="org.springframework.stereotype.controller" />
  </context:component-scan> 

再次执行单元测试,ok。

二. mapper注入失败使用@autowired注解: nested exception is org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [org.byron4j.mcp.mapper.taskermapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}

警告: exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.beancreationexception: error creating bean with name 'tasklistenerconfigcontroller': injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.beancreationexception: could not autowire field: org.byron4j.mcp.service.tasklistenerconfigservice org.byron4j.mcp.controller.tasklistenerconfigcontroller.tasklistenerconfigservice; nested exception is org.springframework.beans.factory.beancreationexception: error creating bean with name 'tasklistenerconfigserviceimpl': injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.beancreationexception: could not autowire field: org.byron4j.mcp.mapper.taskermapper org.byron4j.mcp.service.impl.tasklistenerconfigserviceimpl.taskermapper; nested exception is org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [org.byron4j.mcp.mapper.taskermapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
  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.servlet.frameworkservlet.configureandrefreshwebapplicationcontext(frameworkservlet.java:663)
  at org.springframework.web.servlet.frameworkservlet.createwebapplicationcontext(frameworkservlet.java:629)
  at org.springframework.web.servlet.frameworkservlet.createwebapplicationcontext(frameworkservlet.java:677)
  at org.springframework.web.servlet.frameworkservlet.initwebapplicationcontext(frameworkservlet.java:548)
  at org.springframework.web.servlet.frameworkservlet.initservletbean(frameworkservlet.java:489)
  at org.springframework.web.servlet.httpservletbean.init(httpservletbean.java:136)
  at javax.servlet.genericservlet.init(genericservlet.java:158)
  at org.apache.catalina.core.standardwrapper.initservlet(standardwrapper.java:1284)
  at org.apache.catalina.core.standardwrapper.loadservlet(standardwrapper.java:1197)
  at org.apache.catalina.core.standardwrapper.load(standardwrapper.java:1087)
  at org.apache.catalina.core.standardcontext.loadonstartup(standardcontext.java:5231)
  at org.apache.catalina.core.standardcontext.startinternal(standardcontext.java:5518)
  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:649)
  at org.apache.catalina.startup.hostconfig.deploywar(hostconfig.java:1081)
  at org.apache.catalina.startup.hostconfig$deploywar.run(hostconfig.java:1877)
  at java.util.concurrent.executors$runnableadapter.call(executors.java:511)
  at java.util.concurrent.futuretask.run(futuretask.java:266)
  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: org.byron4j.mcp.service.tasklistenerconfigservice org.byron4j.mcp.controller.tasklistenerconfigcontroller.tasklistenerconfigservice; nested exception is org.springframework.beans.factory.beancreationexception: error creating bean with name 'tasklistenerconfigserviceimpl': injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.beancreationexception: could not autowire field: org.byron4j.mcp.mapper.taskermapper org.byron4j.mcp.service.impl.tasklistenerconfigserviceimpl.taskermapper; nested exception is org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [org.byron4j.mcp.mapper.taskermapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}
  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)
  ... 33 more

解决第一个问题,单元测试后,部署该项目启动时,疯狂报错nested exception is org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [org.byron4j.mcp.mapper.taskermapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)}。多次检查,发现springmvc配置文件中也需要加载mybatis配置,前面我仅仅在spring上下文核心配置文件(spring_ynyn_dependence.xml)中做了配置。

在spring-mvc.xml文件最后引入加载mybatis配置:

<import resource="classpath*:byron4j_ynyn_datasource.xml"/>

重新部署应用启动服务无措,发起请求验证ok。