zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    fastjson反序列化(win环境复现)
    Struts2 S2-059 远程代码执行漏洞
    4. redis未授权漏洞复现(主从复制)
    3. redis未授权漏洞复现(写入linux计划任务反弹shell)
    2. redis未授权漏洞复现(写入webshell)tomcat环境失败
    1. redis未授权漏洞复现(写入公钥利用)
    CVE-2020-1938漏洞复现(文件读取文件包含)
    buuctf-[网鼎杯 2018]Fakebook 1
    buucitf-[极客大挑战 2020]Roamphp1-Welcome
    buuctf-web-[极客大挑战 2019]BuyFlag 1
  • 原文地址:https://www.cnblogs.com/likailan/p/3459776.html
Copyright © 2011-2022 走看看