spring-整合druid阿里巴巴连接池

pom.xml


    4.0.0
    com.gq
    spring-ioc-v2
    0.0.1-SNAPSHOT
    
        
            org.springframework
            spring-context
            4.3.9.RELEASE
        
        
            junit
            junit
            4.12
        
        
        
            mysql
            mysql-connector-java
            5.1.40
        
        
            com.alibaba
            druid
            1.0.23
        
    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    

/spring-ioc-v2/src/main/resources/config.properties

jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///test
jdbcUsername=root
jdbcPassword=123456

/spring-ioc-v2/src/main/resources/spring-configs.xml

注意拷贝(复制)set方法后面的名字

spring-整合druid阿里巴巴连接池_第1张图片

spring-整合druid阿里巴巴连接池_第2张图片

spring-整合druid阿里巴巴连接池_第3张图片


    
    
    
    
    
    
    
        
        
        
        
         
        
    

package test;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Assert;
import org.junit.Test;

import com.alibaba.druid.pool.DruidDataSource;

public class TestDataSource03 extends TestBase{
    @Test
    public void testDruidDataSource() throws SQLException{
        //获取bean对象
         DruidDataSource ds = ctx.getBean("druid",DruidDataSource.class);
        //用接口接收对象,测试对象值是否为空
        Assert.assertNotEquals(null, ds);
        //通过bean对象获取与数据库的连接
        Connection conn = ds.getConnection();
        System.out.println(conn);
        conn.close();
    }
}
九月 04, 2018 2:55:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6ed3ef1: startup date [Tue Sep 04 14:55:02 CST 2018]; root of context hierarchy
九月 04, 2018 2:55:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-configs.xml]
OpenDataSource.OpenDataSource()-2
OpenDataSource.init()
九月 04, 2018 2:55:02 下午 com.mchange.v2.log.MLog 
信息: MLog clients using java 1.4+ standard logging.
九月 04, 2018 2:55:02 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
九月 04, 2018 2:55:02 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
com.mysql.jdbc.JDBC4Connection@419c5f1a
九月 04, 2018 2:55:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6ed3ef1: startup date [Tue Sep 04 14:55:02 CST 2018]; root of context hierarchy
九月 04, 2018 2:55:02 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} closed
OpenDataSource.destory()

你可能感兴趣的:(spring-整合druid阿里巴巴连接池)