如果你曾经或正在使用MyBatis或iBatis,那么,根据java.lang.NoClassDefFoundError: org.apache.ibatis.transaction.TransactionFactory这个异常就可以很容易知道是因为缺少iBatis的jar包造成的错误。解决办法就是下载ibatis,然后将jar文件加入到项目的classpath下面。
问题信息:
2015-1-31 21:25:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [activiti.cfg.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'processEngineConfiguration' defined in class path resource [activiti.cfg.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/ibatis/transaction/TransactionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:890)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.activiti.engine.impl.cfg.BeansConfigurationHelper.parseProcessEngineConfiguration(BeansConfigurationHelper.java:36)
at org.activiti.engine.impl.cfg.BeansConfigurationHelper.parseProcessEngineConfigurationFromResource(BeansConfigurationHelper.java:48)
at org.activiti.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResource(ProcessEngineConfiguration.java:185)
at org.activiti.engine.ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault(ProcessEngineConfiguration.java:177)
at com.bug315.ActivitiTest01.main(ActivitiTest01.java:10)
Caused by: java.lang.NoClassDefFoundError: org/apache/ibatis/transaction/TransactionFactory
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.getDeclaredConstructor(Class.java:1985)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:64)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:938)
... 12 more
Caused by: java.lang.ClassNotFoundException: org.apache.ibatis.transaction.TransactionFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 18 more
解决办法:
将mybatis的jar包导入到当前项目的classpath下面。因为activiti内部使用mybatis来进行数据库操作。
重要提示:
只要遇到抛出java.lang.ClassNotFoundException这个异常,那么肯定是你项目缺少必要的依赖jar包,你需要将依赖jar包导入到项目的classpath下面即可解决。
下面是ClassNotFoundException异常的定义:
public class ClassNotFoundException extends Exception
当应用程序试图使用以下方法通过字符串名加载类时,抛出该异常:
Class 类中的 forName 方法。
ClassLoader 类中的 findSystemClass 方法。
ClassLoader 类中的 loadClass 方法。
但是没有找到具有指定名称的类的定义。
从 1.4 版本开始,此异常已经更新,以符合通用的异常链机制。在构造时提供并通过 getException() 方法访问的“加载类时引发的可选异常”,现在被称为原因,它可以通过 Throwable.getCause() 方法以及与上面提到的“遗留方法”来访问。