五、数据源置换 Spring实现的DriverManagerDataSource并没有提供连接池的功能,只是用来作简单的单机连接测试,并不适合使用于真正的项目当中,可以考虑用比较成熟的数据连接池来取代。Apache DBCP连接池是不错,如要要替换,则需要加载DBCP相关的工具包。 <?xml version="1.0" encoding="gb2312"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!--数据库的数据源定义--> <bean id="rptds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/testdb</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>leizhimin</value> </property> </bean> </beans> 现在所使用的是org.apache.commons.dbcp.BasicDataSource作为注入的DataSource实例,为了使用DBCP 的功能,您需要在Classpath路径中设定commons-dbcp.jar、commons-pool.jar与commons- collections.jar。注意到在dataSource上设定了"destroy-method"属性,如此可以确保BeanFactory在关闭时也一并关闭BasicDataSource。 六、使用JNDI数据源 如果您的Servlet容器提供了JNDI(Java Naming and Directory Interface)的DataSource,您也可以简单的换上这个DataSource: <?xml version="1.0" encoding="gb2312"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!--数据库的数据源定义--> <bean id="dataSource" class="org.springframework.indi.JndiObjectFactoryBean"> <property name="jndiName"> <value>jdbc/testds</value> </property> </bean> </beans> 为了使用org.springframework.indi.JndiObjectFactoryBean,您需要spring-context.jar,"jndiName"实际上要根据您所设定的JNDI查询名称。