将Spring与MyBatis一起使用时的Java异常

时间:2015-12-23 15:44:40

标签: java spring mybatis spring-3 spring-mybatis

进入httpInvoker后我遇到异常,它看起来像这样:

    INFO: Initializing Spring FrameworkServlet 'httpInvoker' pro 23, 2015
 4:34:04 PM org.apache.catalina.core.ApplicationContext log SEVERE:
 StandardWrapper.Throwable java.lang.NoClassDefFoundError:
 org.springframework.beans.FatalBeanException  at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
   at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
   at
 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
   at
 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
   at
 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
   at
 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
   at
 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
   at
 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
   at
 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
   at
 org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
   at
 org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
   at
 org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
   at
 org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
   at
 org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
   at
 org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
   at javax.servlet.GenericServlet.init(GenericServlet.java:158)   at
 org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
   at
 org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
   at
 org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
   at
 org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4997)
   at
 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5289)
   at
 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
   at
 org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
   at
 org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
   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)

 pro 23, 2015 4:34:04 PM org.apache.catalina.core.StandardContext
 loadOnStartup SEVERE: Servlet [httpInvoker] in web application
 [/services] threw load() exception java.lang.NoClassDefFoundError:
 org.springframework.beans.FatalBeanException  at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
   at
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
   at
 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
   at
 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
   at
 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
   at
 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
   at
 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
   at
 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
   at
 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
   at
 org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
   at
 org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
   at
 org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
   at
 org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
   at
 org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
   at
 org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
   at javax.servlet.GenericServlet.init(GenericServlet.java:158)   at
 org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
   at
 org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
   at
 org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
   at
 org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4997)
   at
 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5289)
   at
 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
   at
 org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
   at
 org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
   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)

经过数小时的研究,我发现spring-db.xml是出现问题的地方。在那里有定义连接到例如com.mchange.v2.c3p0.ComboPooledDataSource或enviromentProperties。我一步一步地尝试,从那里逐行评论/解开一些东西,并且有部分定义如下:

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

    <!-- define the SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="hr.test.model" />
    </bean>


    <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="hr.test.dao.api.mappers" />
    </bean>

当我评论调用configurer的最后一个bean时,一切都顺利通过(毫无例外)。

如何停止获取该异常?也许我必须以其他方式编写那个bean? (抱歉没有给出完整的代码,我只能给它...)

我正在使用: Eclipse火星 Java 1.8 春季3.1 MyBatis的 32位Windows

1 个答案:

答案 0 :(得分:0)

如果您对以下行

发表评论,则不会显示上述异常
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="hr.test.dao.api.mappers" />
    </bean>

然后我相信你必须错过项目中的mybatis-spring.jar。请尝试在您的项目中包括以下内容: -

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.2.3</version>
</dependency>