使用Spring能够简化数据源的配置,配置数据源的方式有四种:1、JNDI方式;2、数据源连接池;3、基于JDBC驱动的数据源;4、使用嵌入式的数据源。
四种配置方式的数据源,可以同时使用。通过配置Spring的profile bean来决定使用哪个bean。
下面是数据源的四种配置方式(关于完整的代码在github上):
(关于JNDI的配置,请看“在tomcat上通过JNDI配置H2的数据源”)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.3.xsd">
<!-- 第一种方式-->
<!--
当设置 resource-ref 为 true 的时候,jndi-name 将会自动添加 java:/comp/env 前缀
-->
<beans profile="production">
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/spittr"
expected-type="javax.sql.DataSource"
resource-ref="true"/>
</beans>
<!-- 方法二: 使用数据源连接池-->
<!---->
<beans profile="development">
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
p:driverClassName="org.h2.Driver"
p:url="jdbc:h2:tcp://localhost/~/Documents/opt/servers/h2databases/spittr"
p:username="spittr"
p:password="spittr"
p:initialSize="5"
p:maxWaitMillis="10000"/>
</beans>
<!-- 方法三; 基于JDBC驱动的数据源-->
<beans profile="dev">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.h2.Driver"
p:url="jdbc:h2:tcp://localhost/~/Documents/opt/servers/h2databases/spittr"
p:username="spittr"
p:password="spittr"
/>
</beans>
<!-- 方法四:使用嵌入式的数据源 -->
<beans profile="qa">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:spittr/db/jdbc/schema.sql"/>
<jdbc:script location="classpath:spittr/db/jdbc/test-data.sql"/>
</jdbc:embedded-database>
</beans>
</beans>
关于激活profile,参考了: