package test01;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.jolbox.bonecp.BoneCPDataSource;
@Configuration
@ComponentScan(basePackages = "test01")//扫描test01包
@PropertySource(value={"classpath:config/jdbc.properties"},ignoreResourceNotFound=true)//读取配置文件,文件可为空
public class SpringConfig {
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.driverClassName}")
private String jdbcDriverClassName;
@Value("${jdbc.username}")
private String jdbcUsername;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean(destroyMethod="close")
public DataSource dataSource(){
BoneCPDataSource boneCPDataSource=new BoneCPDataSource();
//数据库驱动
boneCPDataSource.setDriverClass(jdbcDriverClassName);
//数据库URL
boneCPDataSource.setJdbcUrl(jdbcUrl);
//数据库名
boneCPDataSource.setUsername(jdbcUsername);
//密码
boneCPDataSource.setPassword(jdbcPassword);
//检查数据库中空闲连接的时间间隔,时间单位为分,取消设置为0
boneCPDataSource.setIdleConnectionTestPeriodInMinutes(60);
//设置每个未使用连接最大存活时间,单位为分,默认为0
boneCPDataSource.setIdleMaxAgeInMinutes(30);
//设置每个分区最大连接数
boneCPDataSource.setMaxConnectionsPerPartition(200);
//设置每个分区最小连接数
boneCPDataSource.setMinConnectionsPerPartition(5);
return boneCPDataSource;
}
@Bean
public UserDao getUserDAO() {
return new UserDao();
}
}