SpringMVC数据库配置报错

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1327)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:594)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1396)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:959)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:683)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
	at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:482)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java)
	at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
	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:4797)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	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: java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
	at org.springframework.util.ClassUtils.forName(ClassUtils.java:247)
	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1348)
	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1319)
	... 19 more

今天在项目启动的时候报这个错误,我一直在思考原因在哪,看样是是使用SpringMVC的数据库配置存在着一些问题

其实我们在使用Spring的时候数据源头的配置需要在两个地方进行配置,首先是web-xml:

  
    org.springframework.web.context.ContextLoaderListener
		
  
  
    contextConfigLocation
    WEB-INF/applicationContext.xml
  
  

首先是配置这样的一个listener,这样在项目启动的时候就会加载web-inf下面applicationContent.xml这个配置文件

那么我们将加载数据的配置就存在这个applicationContent.xml文件里面:

	
	
		
		
		
		
	
	
		
	

pom.xml里面也需要引入:

	
			commons-dbcp
			commons-dbcp
			1.2.2
		

当然你不是maven项目的话,可以自己去下载这样的jar,上面的三个地方配置好了之后基本上可以实现数据库的连接了,

希望对你有所帮助


你可能感兴趣的:(java)