关于Spring配置 (Cannot find class [org.apache.commons.dbcp.BasicDataSource] 问题)
myeclipse抛出异常
1 org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in URL [file:/opt/app-w/apache-tomcat-7.0.54/webapps/telemetry/WEB-INF/classes/spring/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource 2 at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1278) 3 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:575) 4 at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1347) 5 at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:913) 6 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:617) 7 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934) 8 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 9 at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410) 10 at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 11 at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 12 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973) 13 at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467) 14 at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 15 at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 16 at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 17 at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632) 18 at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247) 19 at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1898) 20 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 21 at java.util.concurrent.FutureTask.run(FutureTask.java:262) 22 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 23 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 24 at java.lang.Thread.run(Thread.java:745) 25 Caused by: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource 26 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) 27 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) 28 at org.springframework.util.ClassUtils.forName(ClassUtils.java:265) 29 at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:419) 30 at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1299) 31 at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1270) 32 ... 22 more 33 2015-01-22 11:16:10 org.springframework.web.context.ContextLoader ERROR Context initialization failed
解决方法:
缺少commons-dbcp.jar、commons-pool.jar这两个包
在pom.xml中加入:
1 <dependency> 2 <groupId>commons-dbcp</groupId> 3 <artifactId>commons-dbcp</artifactId> 4 <version>1.4</version> 5 </dependency> 6 7 <dependency> 8 <groupId>commons-pool</groupId> 9 <artifactId>commons-pool</artifactId> 10 <version>1.6</version> 11 </dependency>