Spring 注入數據源

1、在項目中添加dataSource所用到的包

dbcp數據源所需包:
    commons-dbcp.jar
    commons-pool.jarjava

C3P0數據源所需包:
    c3p0-0.9.1.2.jarspring

2、在須要用到數據源的類中添加DataSource屬性和相應的set方法。

public class StudentDaoImpl implements IStudentDao {
    //數據庫類DataSource在javax.sql包下
    private DataSource dataSource;

    public void save(Student student) {
        Connection conn=null;
         try {
             //經過getConnection()方法獲得數據庫鏈接
             conn = dataSource.getConnection();
             //後面的代碼和JDBC同樣
             PreparedStatement pStat = conn.prepareStatement("insert into student values (?,?)");
             pStat.setInt(1, student.getId());
             pStat.setString(2, student.getName());
             pStat.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("save:"+student.getName());
    }

    //爲dataSource屬性添加set方法,這樣Spring才能把具體的數據源對象注入進來
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

}

3、在Spring配置文件中添加DataSource的bean,並注入到用到DataSource的類中

<?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-2.5.xsd">

    <!-- 數據源:C3P0 -->
    <bean id="dataSource" destroy-method="close"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 基本屬性:數據庫驅動類、鏈接字符串、用戶名、密碼 -->
        <property name="driverClass" value="oracle.jdbc.OracleDriver" />
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
        <property name="user" value="hib" />
        <property name="password" value="hib" />
        <!-- 鏈接數、最小鏈接數、最大鏈接數、最大空閒時間 -->
        <property name="initialPoolSize" value="200"></property>
        <property name="minPoolSize" value="50"></property>
        <property name="maxPoolSize" value="300"></property>
        <property name="maxIdleTime" value="60"></property>
    </bean>
    <!-- dbcp數據源配置 -->
    <!-- 
    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> 
        <property name="driverClassName" value="oracle.jdbc.OracleDriver"/> 
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/> 
        <property name="username" value="hib"/> 
        <property name="password" value="hib"/> 
    </bean> -->

    <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
        <!-- 把dataSource注入給studentDao -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao" />
    </bean>

    <bean id="start" class="com.startspring.Start">
        <property name="studentService" ref="studentService" />
    </bean>
</beans>

------------------------------------------------------------------------------------------------------------------------------sql

補充:把數據源的屬性值寫到properties文件中。數據庫

1、在項目裏添加properties文件,以下:apache

oracle_c3p0.properties:oracle

jdbc.driverClass=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:ORCL
jdbc.user=hib
jdbc.password=hib

key值能夠隨便起,value值就是數據源的屬性值this

2、在Spring配置文件中添加propertyConfigurer bean,並把屬性值用佔位符代替,以下:url

<?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-2.5.xsd">

    <!-- 添加propertyConfigurer bean 這個類也是Spring提供的 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <!-- 把properties文件的路徑注入給location屬性,前面要加classpath: -->
        <property name="location" value="classpath:oracle_c3p0.properties"/>
    </bean>

    <!-- 數據源:C3P0 -->
    <bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 如今能夠使用${} 佔位符了,Spring會去取相應的value值 -->
        <property name="driverClass" value="${jdbc.driverClass}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
        <!-- 把dataSource注入給studentDao -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao" />
    </bean>

    <bean id="start" class="com.startspring.Start">
        <property name="studentService" ref="studentService" />
    </bean>
</beans>
相關文章
相關標籤/搜索