管理DruidDataSource对象
1、引入坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
2、创建bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 管理DruidDataSource对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value=""/>
<property name="url" value=""/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
</beans>
管理c3p0 DataSource对象
1、引入坐标
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>
2、创建bean
<!-- 管理c3p0DataSource对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value=""/>
<property name="jdbcUrl" value=""/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
加载properties文件
1、开启context命名空间
2、使用context空间加载properties文件
3、使用属性占位符${}读取properties文件中的属性
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1、开启context命名空间-->
<!-- 2、使用context空间加载properties文件-->
<context:property-placeholder location="jdbc.properties"/>
<!-- 3、使用属性占位符${}读取properties文件中的属性-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
使用context空间加载properties文件时,可以添加system-properties-mode="NEVER",这样便不会加载系统变量属性,防止properties中命名跟系统变量冲突
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
加载多个properties可使用逗号分隔
<context:property-placeholder location="jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/>
或者加载全部的properties:::location="classpath:*.properties"
<context:property-placeholder location="classpath:*.properties" system-properties-mode="NEVER"/>
或者加载引用的jar包中的的properties:::location="classpath*:*.properties"
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>