遇到一个spring启动时类找不到的问题~
今天将一个老的项目部署到Tomcat7上运行时,spring初始化一直失败,提示错误如下:
Java.lang.NoClassDefFoundError:org.springframework.beans.FatalBeanException
控制台打印的堆栈信息如下:
[org.springframework.web.context.ContextLoader]-[ERROR] Context initialization failed java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
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.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:434)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4738)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1780)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
解决过程:
1. 可能是类未被添加到buildpath或类未被正确引入到tomcat的部署路径下。
ctrl+shift+T搜索,在spring-beans-3.2.0.RELEASE.jar这个包下找到了这个类,确认jar包已被添加到项目的bulidpath中了。
接着去tomcat的项目部署路径下查看,发现项目下有这个jar包,将jar包解压,的确有FatalBeanException类。
如此奇怪,类确实存在,为什么还会报类找不到的错误?
2. 跟踪spring源码,查看抛出异常的具体位置。
通过错误提示信息上描述的doCreateBean方法错误,找到spring-beans-3.2.0.RELEASE.jar下对应的这个方法,在该方法上打断点。
然后以Debug方式启动,程序运行到上面的断点上,一步步跟踪,发现出问题的地方了:
Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { exposedObject = initializeBean(beanName, exposedObject, mbd); } } catch (Throwable ex) { if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex; } else {
//这里的ex对象,实际上是一个java.lang.StackOverflowError,即:栈溢出错误。但是控制台上给出的提示信息却是:NoClassDefFoundError。 throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } }
找到问题的原因了:即JVM的栈设置小了,出现栈溢出。
3. 调整tomcat启动时JVM的栈内存大小,该问题就解决了。
Eclipse-->windows-->preference-->java-->install jres-->选择JDK-->edit-->添加启动参数-Xss16m