Spring 注入數據源


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

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

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

二、在需要用到數據源的類中添加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;
    }

}

三、在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>

------------------------------------------------------------------------------------------------------------------------------

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

一、在項目里添加properties文件,如下:

oracle_c3p0.properties:

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

key值可以隨便起,value值就是數據源的屬性值

二、在Spring配置文件中添加propertyConfigurer 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-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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM