配置连接池的信息
public class DruidProp {
private String url;
private String username;
private String password;
private String driverClassName;
//初始化大小,最小,最大
private Integer initialSize;
private Integer minIdle;
private Integer maxActive;
//配置获取连接等待超时的时间
private Long maxWait;
//配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
private Long timeBetweenEvictionRunsMillis;
//配置一个连接在池中最小生存的时间,单位是毫秒
private Long minEvictableIdleTimeMillis;
private String validationQuery;
private Boolean testWhileIdle;
private Boolean testOnBorrow;
private Boolean testOnReturn;
//打开PSCache,并且指定每个连接上PSCache的大小
private Boolean poolPreparedStatements;
private Integer maxPoolPreparedStatementPerConnectionSize;
//配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
private String filters;
@PostConstruct
public void init(){
//默认用mysql
if(driverClassName == null) driverClassName = "com.mysql.jdbc.Driver";
if(initialSize == null) initialSize = 8;
if(minIdle == null) minIdle = 1;
if(maxActive == null) maxActive = 20;
if(maxWait == null) maxWait = 60000L;
if(timeBetweenEvictionRunsMillis == null) timeBetweenEvictionRunsMillis = 60000L;
if(minEvictableIdleTimeMillis == null) minEvictableIdleTimeMillis = 300000L;
if(validationQuery == null) validationQuery = "select 1 from dual";
if(testWhileIdle == null) testWhileIdle = true;
if(testOnBorrow == null) testOnBorrow = false;
if(testOnReturn == null) testOnReturn = false;
if(poolPreparedStatements == null) poolPreparedStatements = true;
if(maxPoolPreparedStatementPerConnectionSize == null) maxPoolPreparedStatementPerConnectionSize = 20;
if(filters == null) filters = "stat, wall";
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
.......get.....set
}
DruidConfig类通过注解@ConfigurationProperties获取数据库连接信息,并通过dataSource()方法设置数据源信息。
druidcfg:
datasource:
url: jdbc:mysql://url:3306/gcloud-seccenter?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true
username: root
password: root
/*
*/
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "druidcfg.datasource")
public DruidProp druidProp(){
return new DruidProp();
}
@Bean
@Primary
public DataSource dataSource(DruidProp druidProp) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(druidProp.getUrl());
dataSource.setUsername(druidProp.getUsername());
dataSource.setPassword(druidProp.getPassword());
dataSource.setDriverClassName(druidProp.getDriverClassName());
dataSource.setInitialSize(druidProp.getInitialSize());
dataSource.setMinIdle(druidProp.getMinIdle());
dataSource.setMaxActive(druidProp.getMaxActive());
dataSource.setTimeBetweenEvictionRunsMillis(druidProp.getTimeBetweenEvictionRunsMillis());
dataSource.setMinEvictableIdleTimeMillis(druidProp.getMinEvictableIdleTimeMillis());
dataSource.setValidationQuery(druidProp.getValidationQuery());
dataSource.setTestWhileIdle(druidProp.getTestWhileIdle());
dataSource.setTestOnBorrow(druidProp.getTestOnBorrow());
dataSource.setTestOnReturn(druidProp.getTestOnReturn());
dataSource.setPoolPreparedStatements(druidProp.getPoolPreparedStatements());
dataSource.setMaxPoolPreparedStatementPerConnectionSize(druidProp.getMaxPoolPreparedStatementPerConnectionSize());
try {
dataSource.setFilters(druidProp.getFilters());
} catch (SQLException e) {
e.printStackTrace();
}
return dataSource;
}
}