DruidDBConfiguration.java
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.viontech.configuration;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DruidDBConfiguration {
private Logger logger = LoggerFactory.getLogger(DruidDBConfiguration.class);
@Value("${spring.datasource.url:127.0.0.1}")
private String dbUrl;
@Value("${spring.datasource.username:postgresql}")
private String username;
@Value("${spring.datasource.password:password}")
private String password;
@Value("${spring.datasource.driverClassName:driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize:0}")
private int initialSize;
@Value("${spring.datasource.minIdle:0}")
private int minIdle;
@Value("${spring.datasource.maxActive:1}")
private int maxActive;
@Value("${spring.datasource.maxWait:0}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis:0}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis:0}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery:0}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle:false}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow:false}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn:false}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements:false}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize:0}")
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
@Value("${spring.datasource.connectionProperties:connectionProperties}")
private String connectionProperties;
@Bean //声明其为Bean实例
@Primary //在同样的DataSource中,首先使用被标注的DataSource
@ConditionalOnExpression("#{'${spring.datasource.driverClassName:false}'!='false'}")
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
if("driverClassName".equals(driverClassName)){
return null;
}
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}