/spring-ioc-v5/src/main/resources/config.properties
jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///jtsys?useUnicode=true&characterEncoding=utf-8
jdbcUsername=root
jdbcPassword=123456
/spring-ioc-v5/src/main/java/config/AppRootConfig.java
package config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import com.alibaba.druid.pool.DruidDataSource;
/**
* 使用此配置类替换spring-configs.xml
* @Configuration 描述这个类是个配置类
* @ComponentScan 用于定义要扫描的具体包
*/
@PropertySource("classpath:config.properties")
public class AppRootConfig {
/**
* 整合第三方bean
* @return
*/
@Bean("dataSource")//假如没有指定名字,默认为方法名
@Lazy(false)
public DataSource newDataSource(@Value("${jdbcDriver}")String driver,
@Value("${jdbcUrl}")String url,
@Value("${jdbcUsername}")String user,
@Value("${jdbcPassword}")String password){
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(user);
ds.setPassword(password);
return ds;
}
}
调用生命周期方法
initMethod="init",destroyMethod="close"
package config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import com.alibaba.druid.pool.DruidDataSource;
/**
* 使用此配置类替换spring-configs.xml
* @Configuration 描述这个类是个配置类
* @ComponentScan 用于定义要扫描的具体包
*/
@Configuration
@PropertySource("classpath:config.properties")
public class AppRootConfig {
/**
* 整合第三方bean
* @return
*/
//@Bean描述这个类的方法由sping管理
@Bean(value="dataSource",initMethod="init",destroyMethod="close")//假如没有指定名字,默认为方法名
@Lazy(false)
public DataSource newDataSource(@Value("${jdbcDriver}")String driver,
@Value("${jdbcUrl}")String url,
@Value("${jdbcUsername}")String user,
@Value("${jdbcPassword}")String password){
//@Value用于读取配置文件中的内容
DruidDataSource ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(user);
ds.setPassword(password);
return ds;
}
}
package test;
import org.junit.After;
import org.junit.Before;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import config.AppRootConfig;
public class TestBase {
protected AnnotationConfigApplicationContext ctx;
@Before
public void init(){
ctx = new AnnotationConfigApplicationContext(AppRootConfig.class);
}
@After
public void close(){
ctx.close();
}
}
package test;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
public class TestDruidDataSource01 extends TestBase {
@Test
public void testDruidDataSource() throws SQLException{
DataSource ds = ctx.getBean("dataSource",DataSource.class);
System.out.println(ds);
System.out.println(ds.getClass().getName());
Connection conn = ds.getConnection();
System.out.println(conn);
conn.close();
}
}
九月 07, 2018 9:17:15 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@16f65612: startup date [Fri Sep 07 09:17:15 CST 2018]; root of context hierarchy
九月 07, 2018 9:17:15 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
{
CreateTime:"2018-09-07 09:17:15",
ActiveCount:0,
PoolingCount:0,
CreateCount:0,
DestroyCount:0,
CloseCount:0,
ConnectCount:0,
Connections:[
]
}
com.alibaba.druid.pool.DruidDataSource
com.mysql.jdbc.JDBC4Connection@e056f20
九月 07, 2018 9:17:16 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@16f65612: startup date [Fri Sep 07 09:17:15 CST 2018]; root of context hierarchy
九月 07, 2018 9:17:16 上午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} closed