利用spring与springmvc对mabytis进行整合时无法注入mapper怎么解决-成都快上网建站

利用spring与springmvc对mabytis进行整合时无法注入mapper怎么解决

利用spring与springmvc对mabytis进行整合时无法注入mapper怎么解决?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

我们提供的服务有:成都网站设计、网站建设、外贸网站建设、微信公众号开发、网站优化、网站认证、平阳ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的平阳网站制作公司

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"?>

  Archetype Created Web Application
  
    index.jsp
  

  
  
    contextConfigLocation
    classpath:spring_ynyn_dependence.xml
  

  
  
    characterEncoding
    org.springframework.web.filter.CharacterEncodingFilter
    
      forceEncoding
      true
    
    
      encoding
      UTF-8
    
  
  
    characterEncoding
    /*
  

  
  
    dispatcherServlet
    
      org.springframework.web.servlet.DispatcherServlet
    
    
      contextConfigLocation
      
        classpath:spring-mvc.xml
      
    
    1
  

  
    dispatcherServlet
    /
  

  
    HiddenHttpMethodFilter
    org.springframework.web.filter.HiddenHttpMethodFilter
  
  
    HiddenHttpMethodFilter
    dispatcherServlet
  

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

springMVC的配置文件spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>



  
    
   

  
  
    
      
        
      
      
        
        
          
            WriteMapNullValue
            WriteNullStringAsEmpty
          
        
      
    
  

  
  
  
  
  
  

  
  
    
    
    
  

sprin框架的配置文件spring_ynyn_dependence.xml

<?xml version="1.0" encoding="UTF-8"?>

  

   
    
  


  
  

  
  

数据源配置文件byron4j_ynyn_datasource.xml

<?xml version="1.0" encoding="UTF-8"?>


  
    
    
    
    
  


  
    
    
  

  
  
    
  

  

  
   
     
     
     
     
      
     
      
     
      
    
      
     
     
     
      
     
      
    
     
     
     
     
     
     
     
     
     
  

mybatis配置文件byron4j_ynyn_mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
  
  

单元测试基类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 )处理,需要使用 进行配置。调整后的配置是:

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

将以下配置内容
 
    
  
替换为:

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


    
   

再次执行单元测试,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配置:

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


新闻名称:利用spring与springmvc对mabytis进行整合时无法注入mapper怎么解决
文章转载:http://kswjz.com/article/pghici.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流