问题背景
前两天公司新开发了一个项目,使用的Spring Boot2.0,而原有的项目部署在Tomcat 7之上,所以需要升级一下Tomcat的版本,在安装新的Tomcat 8,并将原有的配置复制到Tomcat 8之后,启动服务会发现一直报一个错误 :
1 2 3 4 5 6 7 8 [ERROR][org.springframework.web.context.ContextLoader] Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tNewPriceApplyDao' defined in ServletContext resource [/WEB-INF/applicationContext-beans.xml] Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext-dataSource.xml : Invocation of init method failed; nested exception is javax.naming.NamingException: 无法加载资源工厂类 [Root exception is java.lang.ClassNotFoundException:org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]
解决方法
看到这个报错ClassNotFoundException:org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory
,第一反应自然是包没有加载进来,或者说没有读取到这个dbcp这个包,去Tomcat 8安装目录下的lib目录下查看,实际上tomcat-dbcp.jar这个包是存在的,那为什么在Tomcat 7里面可以加载这个jar包,在Tomcat 8就不行了呢?
此为Tomcat 8的lib目录:
我们来看一下Tomcat 7关于javax.sql.DataSource的指定方式为org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory。Apache Tomcat 7配置参考 。
在原来Tomcat 7的conf目录下context.xml文件中是这样进行配置的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <Resource name="jdbc/komDB" type="javax.sql.DataSource" factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory" auth="Container" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://xxx/komdb?characterEncoding=utf-8&useUnicode=true&autoReconnect=true" username="xxx" password="xxx maxActive="100" maxIdle="100" maxWait="6000" initialSize="20" minIdle="20" validationQuery="SELECT 1" testWhileIdle="true" testOnBorrow="false" timeBetweenEvictionRunsMillis="60000" minEvictableIdleTimeMillis="1800000" numTestsPerEvictionRun="10" removeAbandoned="true" removeAbandonedTimeout="180" />
接着来看一下Tomcat 8关于javax.sql.DataSource的指定方式为org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory。Apache Tomcat 8配置参考 。
到这里终于破案了,在Tomcat 7和Tomcat 8之间更改了默认工厂的名称。一些属性名称也发生了变化。所以在升级Tomcat的时候有很多细节地方需要注意,将这里改为Tomcat 8的指定方式后,重启Tomcat,该jar包成功加载。
同时需注意Tomcat 7中配置context.xml时指定的最大线程,最大等待时间等参数名,在Tomcat 8中均有相应的调整,例如maxActive需改写为maxTotal,maxWait需改写为maxWaitMillis,当然,还有其他属性,在这里不做赘述,在Tomcat启动日志中会以warning的形式将过时的参数抛出,届时按warning提示修改即可。
更改后的Tomcat 8的conf目录下context.xml的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <Resource name="jdbc/komDB" type="javax.sql.DataSource" factory="org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory" auth="Container" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://xxx/komdb?characterEncoding=utf-8&useUnicode=true&autoReconnect=true" username="xxx" password="xxx" maxTotal="100" maxIdle="100" maxWaitMillis="6000" initialSize="20" minIdle="20" validationQuery="SELECT 1" testWhileIdle="true" testOnBorrow="false" timeBetweenEvictionRunsMillis="60000" minEvictableIdleTimeMillis="1800000" numTestsPerEvictionRun="10" removeAbandonedOnBorrow="true" removeAbandonedTimeout="180" />
至于网上描述的其他方式,例如引入commons-dbcp2.jar包、commons-pool2.jar包等,笔者通过更改相应的context.xml中的factory属性为org.apache.commons.dbcp.BasicDataSourceFactory仍未成功,可能也是指定的引用路径不对,在此不做深究。