SoFunction
Updated on 2025-03-01

Solution to the problem of mapper injection failure when spring+springmvc integrates mabytis

I haven't built a architecture for a long time. Today, when I used spring+springmvc to integrate mabytis, two tricks happened.

Spend a night to solve both problems. Now I will summarize the wrong experience and share it to avoid jumping in when encountering pitfalls in the future.

1. During unit testing, the unit test fails and reports an error: A ServletContext is required to configure default servlet handling

October 28, 2016 9:03:33 afternoon  refresh
warn: Exception encountered during context initialization - cancelling refresh attempt
: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is : A ServletContext is required to configure default servlet handling
  at (:599)
  at (:1119)
  at (:1014)
  at (:504)
  at (:476)
  at $(:303)
  at (:230)
  at (:299)
  at (:194)
  at (:755)
  at (:757)
  at (:480)
  at (:125)
  at (:60)
  at (:109)
  at (:261)
  at (:68)
  at (:86)
  at (:72)
  at (:117)
  at (:83)
  at (:212)
  at .junit4.(:200)
  at .junit4.SpringJUnit4ClassRunner$(:259)
  at (:12)
  at .junit4.(:261)
  at .junit4.(:219)
  at .junit4.(:83)
  at $(:290)
  at $(:71)
  at (:288)
  at $000(:58)
  at $(:268)
  at .(:61)
  at .(:68)
  at (:363)
  at .junit4.(:163)
  at ..(:50)
  at (:38)
  at (:459)
  at (:675)
  at (:382)
  at (:192)
Caused by: : Failed to instantiate []: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is : A ServletContext is required to configure default servlet handling
  at (:189)
  at (:588)
  ... 42 more

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http:///2001/XMLSchema-instance" xmlns="/xml/ns/javaee" xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>

  <!-- SpecifySpringInitial context-Specifyspring-coreConfiguration File -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring_ynyn_dependence.xml</param-value>
  </context-param>

  <!-- Character Encoding Filter -->
  <filter>
    <filter-name>characterEncoding</filter-name>
    <filter-class></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分发器配置并Specifyspring-mvcConfiguration File -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>
      
    </servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        classpath:
      </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></filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <servlet-name>dispatcherServlet</servlet-name>
  </filter-mapping>
</web-app>

The resources and configuration files are placed in the src/main/respources directory.

springMVC configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
  xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:context="/schema/context"
  xmlns:mvc="/schema/mvc" xmlns:util="/schema/util"
  xmlns:aop="/schema/aop" xmlns:task="/schema/task"
  xsi:schemaLocation="
      /schema/beans 
      /schema/beans/  
      /schema/context 
      /schema/context/  
      /schema/mvc 
      /schema/mvc/
      /schema/util 
      /schema/util/spring-util-3.
      /schema/aop 
      /schema/aop/spring-aop-2.
      /schema/task 
      /schema/task/">


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

  <!-- Support for default annotation mapping -->
  <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
      <bean class="">
        <constructor-arg value="UTF-8" />
      </bean>
      <bean
        class="">
        <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>

  <!-- Static resource mapping -->
  <mvc:resources mapping="/js/**" location="/js/" />
  <mvc:resources mapping="/css/**" location="/css/" />
  <mvc:resources mapping="/images/**" location="/images/" />
  <!-- When the static resources to be accessed above are not included in the above configuration,Then access it according to this configuration -->
  <mvc:default-servlet-handler />

  <!--Analysis of model view name,That is, add a prefix to the model view name -->
  <bean
    class="">
    <property name="viewClass"
      value="" />
    <property name="prefix" value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
  </bean>
</beans>

Spring framework configuration file spring ynyn dependence.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
  xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:aop="/schema/aop"
  xmlns:context="/schema/context"
  xmlns:util="/schema/util"
  xmlns:task="/schema/task"
  xsi:schemaLocation="
      /schema/beans 
      /schema/beans/spring-beans-3.
      /schema/aop 
      /schema/aop/spring-aop-3.
      /schema/context
      /schema/context/spring-context-3.
      /schema/util 
      /schema/util/spring-util-3.
      /schema/task 
      /schema/task/">
  <util:properties  location="classpath:" />

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


  <!--Introducing timing task annotation driver,Can be used in the application@ScheduledAnnotation development timing tasks-->
  <task:annotation-driven />

  <!--Introduce data source configuration,includemybatisRelated configurations-->
  <import resource="classpath*:byron4j_ynyn_datasource.xml"/>
</beans>

Data source configuration file byron4j_ynyn_datasource.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
  xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:aop="/schema/aop"
  xmlns:context="/schema/context"
  xmlns:tx="/schema/tx" 
  xsi:schemaLocation="
      /schema/beans 
      /schema/beans/spring-beans-3.
      /schema/aop 
      /schema/aop/spring-aop-3.
      /schema/context
      /schema/context/spring-context-3.
      /schema/tx  
      /schema/tx/spring-tx-3.">

  <bean  class="">
    <property name="configLocation" value="classpath:byron4j_ynyn_mybatis" />
    <!--My project's table file is placedsrc/main/resourcesIn the directory-->
    <property name="mapperLocations" value="classpath*:sqlmap/*.xml" />
    <property name="dataSource" ref="dataSource" />
  </bean>


  <bean class="">
    <property name="basePackage" value="org." />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  </bean>

  <!-- Transaction Management -->
  <bean 
    class="">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

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

  <!--The one used here issqlitedatabase,是一种文件database,Accounts with username and password must have permission to read and write files-->
  <bean  
     class="" 
     destroy-method="close"> 
    <property name="driverClassName" value=""/> 
    <property name="url" value="jdbc:sqlite:E:\"/> 
    <property name="username" value="username"/> 
    <property name="password" value="password"/> 
    <!--maxActive: Maximum number of connections-->  
    <property name="maxActive" value="200"/> 
    <!--minIdle: Minimum idle connection-->  
    <property name="minIdle" value="10"/> 
    <!--maxIdle: Maximum idle connection-->  
    <property name="maxIdle" value="50"/>
    <!--initialSize: Initialize the connection-->  
    <property name="initialSize" value="50"/> 
    <!-- Whether to print when the connection is leaked --> 
    <property name="logAbandoned" value="true"/> 
    <!--removeAbandoned: Whether to automatically recycle timeout connections-->  
    <property name="removeAbandoned" value="true"/> 
    <!--removeAbandonedTimeout: Timeout(In seconds)-->  
    <property name="removeAbandonedTimeout" value="180"/>
    <!--maxWait: Timeout waiting time in milliseconds 1000equal60Second--> 
    <property name="maxWait" value="1000"/> 
    <!-- The value of sleeping during the idle connection recycler thread running,以毫Second为单位. --> 
    <property name="timeBetweenEvictionRunsMillis" value="10000"/> 
    <!-- Recycler threads are connected every time(If there is)Number of connections checked at runtime --> 
    <property name="numTestsPerEvictionRun" value="10"/> 
    <!-- 1000 * 60 * 30 Connections remain idle in the pool without being idle connected to the recycler thread--> 
    <property name="minEvictableIdleTimeMillis" value="10000"/> 
    <property name="validationQuery" value="SELECT 1 LIMIT 1 "/> 
  </bean>

</beans>

mybatis configuration file byron4j_ynyn_mybatis

<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE configuration
    PUBLIC "-////DTD Config 3.0//EN"
      "/dtd/">
  <configuration>

</configuration>

Unit test base class

In the src/test/Java directory:

package org.;

import ;
import ;
import ;
import ;
import ;
import .junit4.SpringJUnit4ClassRunner;

/**
  * Mock test class base class--configuration file loading class
  *
  * @ClassName: BaseSpringTestCase
  *
  */
@RunWith()
@ContextConfiguration(locations = { "classpath:spring_ynyn_dependence.xml" })
public class BaseMockitoTestCase {
  @Before
  public void beforeInvoke() {
    ("Work before call...");
    (this);
  }

  @After
  public void afterInvoke() {
    ("The call is completed.");
  }

}

There is something wrong with the unit test class

package org.;

import ;
import ;

import org.;
import org.;
import org.;



public class StudentTest extends BaseMockitoTestCase{

  @Autowired
  StudentMapper studentMapper;


  @Test
  public void selectTest4Success(){
    Student record = new Student();
    (1);
    Student resultStudent = (record);
    (resultStudent);;
  }
}

When executing unit tests, an error warning was reported: Exception encountered during context initialization - cancelling refresh attempt
: Error creating bean with name ‘defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method ‘defaultServletHandlerMapping' threw exception; nested exception is : A ServletContext is required to configure default servlet handling

At this time, check the two configuration files of spring-core and springMVC. The exception prompt message is that the default servletContext needs to be configured with the default servlet processing mapper. He looked confused. . . Finally, I found that there is a problem with the context and package scanning configuration of the two configuration files. The springServlet context should be handled in the spring core configuration ( spring_ynyn_dependence.xml ) and needs to be configured using <context:annotation-config />. The adjusted configuration is:

Adjustment of the framework's configuration file spring_ynyn_dependence.xml

Configure the following configuration content
&lt;context:component-scan base-package="org..*" &gt; 
    &lt;context:exclude-filter type="annotation" expression="" /&gt;
  &lt;/context:component-scan&gt;
Replace with:
&lt;context:annotation-config /&gt;

Configuration file adjustment

<context:component-scan base-package=".*" >
    <context:include-filter type="annotation" expression="" />
  </context:component-scan> 

Execute the unit test again, OK.

2. Mapper injection failed to use @Autowired annotation: nested exception is : No qualifying bean of type [org.] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@(required=true)}

warn: Exception encountered during context initialization - cancelling refresh attempt
: Error creating bean with name 'taskListenerConfigController': Injection of autowired dependencies failed; nested exception is : Could not autowire field: org. org.; nested exception is : Error creating bean with name 'taskListenerConfigServiceImpl': Injection of autowired dependencies failed; nested exception is : Could not autowire field: org. org.; nested exception is : No qualifying bean of type [org.] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@(required=true)}
  at (:334)
  at (:1210)
  at (:537)
  at (:476)
  at $(:303)
  at (:230)
  at (:299)
  at (:194)
  at (:755)
  at (:757)
  at (:480)
  at (:663)
  at (:629)
  at (:677)
  at (:548)
  at (:489)
  at (:136)
  at (:158)
  at (:1284)
  at (:1197)
  at (:1087)
  at (:5231)
  at (:5518)
  at (:150)
  at (:901)
  at (:877)
  at (:649)
  at (:1081)
  at $(:1877)
  at $(:511)
  at (:266)
  at (:1142)
  at $(:617)
  at (:745)
Caused by: : Could not autowire field: org. org.; nested exception is : Error creating bean with name 'taskListenerConfigServiceImpl': Injection of autowired dependencies failed; nested exception is : Could not autowire field: org. org.; nested exception is : No qualifying bean of type [org.] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@(required=true)}
  at $(:561)
  at (:88)
  at (:331)
  ... 33 more

The first problem was solved. After unit testing, the nested exception is: No qualifying bean of type [org.] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@(required=true)}. After checking many times, I found that the mybatis configuration also needs to be loaded in the springMVC configuration file. I just made configurations in the spring context core configuration file (spring_ynyn_dependence.xml).

Introduce loading mybatis configuration at the end of the file:

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

Re-deploy the application startup service and make a request to verify OK.