pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gq</groupId>
<artifactId>spring-ioc-v2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 整合c3p0连接池 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.23</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
/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方法后面的名字
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--如何理解spring中的Bean对象:由spring框架管理的所有对象都是bean对象 -->
<!-- Bean对象何时创建?要看对象是否配置延迟加载 -->
<!-- Bean对象的默认作用域singleton(默认在内存中只有一份) -->
<!-- 单例对象是何时销毁的?容器关闭时销毁 -->
<!-- 系统底层会读取配置文件,并将配置信息封装到Properties对象(本质上是一个map)-->
<util:properties id="cfg" location="classpath:config.properties"></util:properties>
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="#{cfg.jdbcDriver}"></property>
<property name="url" value="#{cfg.jdbcUrl}"></property>
<property name="username" value="#{cfg.jdbcUsername}"></property>
<property name="password" value="#{cfg.jdbcPassword}"></property>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="1800" />
</bean>
</beans>
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 <clinit>
信息: 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()