什么是数据源
数据源(DataSource)是SUN公司制定的用于获取数据库连接的规范接口。它存在于 javax.sql包中,用来代替 DriverManager 的方式来获取连接。
DataSource 与 DriverManager 获取连接的不同:
a)、DriverManager是由SUN公司实现的,它只供了最基本的获取连接的方式;
b)、DataSource是一个接口,不光SUN可以实现,很多第三方的中间件也可以实现
DataSource一般有如下三种实现方式:
a)、标准实现 -- 提供最基本的连接,也就是DriverManager的方式;
b)、连接池的实现 -- 提供了连接池,是一种可以缓存及管理多个数据库连接的“容器”;
c)、分布事务的实现 -- 提供了连接池,而且这个池中的连接是支持分布式事务的(Distribute Transaction)
Spring提供了4种配置数据源的方式:
- Spring自带的数据源(org.springframework.jdbc.datasource.DriverManagerDataSource)
- DBCP数据源
- C3P0数据源
- JNDI数据源
配置DriverManagerDataSource
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
配置DBCP数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:DEV" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
BasicDataSource提供了close()方法关闭数据源,所以必须设定destroy-method=”close”属性, 以便Spring容器关闭时,数据源能够正常关闭
配置C3P0数据源
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value=" oracle.jdbc.driver.OracleDriver "/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:DEV"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
ComboPooledDataSource和BasicDataSource一样提供了一个用于关闭数据源的close()方法,这样我们就可以保证Spring容器关闭时数据源能够成功释放
配置JNDI数据源
<beans xmlns=http://www.springframework.org/schema/beans
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:jee=http://www.springframework.org/schema/jee
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/orclight"/>
</bean>
<jee:jndi-lookup id="dataSource" jndi-name=" java:comp/env/jdbc/orclight"/>
</beans>
如果在 <jee:jndi-lookup>
元素里面添加resource-ref=true
,这样给定的jndi-name
将会自动添加java:comp/env/
前缀
位于 jee 命名空间下的
根据环境装配bean
<beans xmlns=http://www.springframework.org/schema/beans
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:jee=http://www.springframework.org/schema/jee
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<beans profile="dev">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/orclight"/>
</bean>
<jee:jndi-lookup id="dataSource" jndi-name=" java:comp/env/jdbc/orclight"/>
</beans>
<beans profile="other">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
</beans>
</beans>
配置好profile之后,下一步则是激活profile,通过设置spring.profiles.active和spring.profiles.default属性,前者优先级会高于后者,且只会存在一个值
有多种方式可以设置该属性:
作为DispatcherServlet的初始化参数
作为WEB应用上下文参数
作为JNDI条目
作为环境变量
作为JVM的系统属性
在集成测试类上,使用@ActiveProfiles注解(需要spring集成测试环境)
- 作为DispatcherServlet的初始化参数
<servlet>
<servlet-name>scdp-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
- 作为WEB应用上下文参数
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>