Skip to content

Commit 5a1dc10

Browse files
committed
解决 druid 属性配置无效的问题
1 parent e93d916 commit 5a1dc10

File tree

6 files changed

+157
-21
lines changed

6 files changed

+157
-21
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
<artifactId>pagehelper-spring-boot-starter</artifactId>
8888
<version>1.1.0</version>
8989
</dependency>
90+
91+
<dependency>
92+
<groupId>org.springframework.boot</groupId>
93+
<artifactId>spring-boot-configuration-processor</artifactId>
94+
<optional>true</optional>
95+
</dependency>
9096
</dependencies>
9197

9298
<build>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package tk.mybatis.springboot.druid;
2+
3+
import com.alibaba.druid.pool.DruidDataSource;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
9+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
import javax.sql.DataSource;
14+
import java.sql.SQLException;
15+
16+
/**
17+
* @author liuzh
18+
* @since 2017/2/5.
19+
*/
20+
@Configuration
21+
@EnableConfigurationProperties(DruidProperties.class)
22+
@ConditionalOnClass(DruidDataSource.class)
23+
@ConditionalOnProperty(prefix = "druid", name = "url")
24+
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
25+
public class DruidAutoConfiguration {
26+
27+
@Autowired
28+
private DruidProperties properties;
29+
30+
@Bean
31+
public DataSource dataSource() {
32+
DruidDataSource dataSource = new DruidDataSource();
33+
dataSource.setUrl(properties.getUrl());
34+
dataSource.setUsername(properties.getUsername());
35+
dataSource.setPassword(properties.getPassword());
36+
if (properties.getInitialSize() > 0) {
37+
dataSource.setInitialSize(properties.getInitialSize());
38+
}
39+
if (properties.getMinIdle() > 0) {
40+
dataSource.setMinIdle(properties.getMinIdle());
41+
}
42+
if (properties.getMaxActive() > 0) {
43+
dataSource.setMaxActive(properties.getMaxActive());
44+
}
45+
dataSource.setTestOnBorrow(properties.isTestOnBorrow());
46+
try {
47+
dataSource.init();
48+
} catch (SQLException e) {
49+
throw new RuntimeException(e);
50+
}
51+
return dataSource;
52+
}
53+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package tk.mybatis.springboot.druid;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
5+
/**
6+
* 只提供了常用的属性,如果有需要,自己添加
7+
*
8+
* @author liuzh
9+
* @since 2017/2/5.
10+
*/
11+
@ConfigurationProperties(prefix = "druid")
12+
public class DruidProperties {
13+
private String url;
14+
private String username;
15+
private String password;
16+
17+
private int maxActive;
18+
private int minIdle;
19+
private int initialSize;
20+
private boolean testOnBorrow;
21+
22+
public String getUrl() {
23+
return url;
24+
}
25+
26+
public void setUrl(String url) {
27+
this.url = url;
28+
}
29+
30+
public String getUsername() {
31+
return username;
32+
}
33+
34+
public void setUsername(String username) {
35+
this.username = username;
36+
}
37+
38+
public String getPassword() {
39+
return password;
40+
}
41+
42+
public void setPassword(String password) {
43+
this.password = password;
44+
}
45+
46+
public int getMaxActive() {
47+
return maxActive;
48+
}
49+
50+
public void setMaxActive(int maxActive) {
51+
this.maxActive = maxActive;
52+
}
53+
54+
public int getMinIdle() {
55+
return minIdle;
56+
}
57+
58+
public void setMinIdle(int minIdle) {
59+
this.minIdle = minIdle;
60+
}
61+
62+
public int getInitialSize() {
63+
return initialSize;
64+
}
65+
66+
public void setInitialSize(int initialSize) {
67+
this.initialSize = initialSize;
68+
}
69+
70+
public boolean isTestOnBorrow() {
71+
return testOnBorrow;
72+
}
73+
74+
public void setTestOnBorrow(boolean testOnBorrow) {
75+
this.testOnBorrow = testOnBorrow;
76+
}
77+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Auto Configure
2+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
3+
tk.mybatis.springboot.druid.DruidAutoConfiguration

src/main/resources/application.yml

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,16 @@ logging:
66
level:
77
tk.mybatis: TRACE
88

9+
druid:
10+
url: jdbc:mysql://192.168.16.137:3306/test
11+
username: root
12+
password:
13+
initial-size: 1
14+
min-idle: 1
15+
max-active: 20
16+
test-on-borrow: true
17+
918
spring:
10-
datasource:
11-
name: test
12-
url: jdbc:mysql://192.168.16.137:3306/test
13-
username: root
14-
password:
15-
# 使用druid数据源
16-
type: com.alibaba.druid.pool.DruidDataSource
17-
driver-class-name: com.mysql.jdbc.Driver
18-
filters: stat
19-
maxActive: 20
20-
initialSize: 1
21-
maxWait: 60000
22-
minIdle: 1
23-
timeBetweenEvictionRunsMillis: 60000
24-
minEvictableIdleTimeMillis: 300000
25-
validationQuery: select 'x'
26-
testWhileIdle: true
27-
testOnBorrow: false
28-
testOnReturn: false
29-
poolPreparedStatements: true
30-
maxOpenPreparedStatements: 20
3119
mvc:
3220
view:
3321
prefix: /templates/

src/main/resources/banner.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
${AnsiColor.BLUE}
2+
__ __ ____ _ _ _____ _ _ _ __ __
3+
| \/ | | _ \ | | (_) ___ | __ \ | | | | | | ___ | \/ |
4+
| \ / |_ _| |_) | __ _| |_ _ ___ ( _ ) | |__) |_ _ __ _ ___| |__| | ___| |_ __ ___ _ __ ( _ ) | \ / | __ _ _ __ _ __ ___ _ __
5+
| |\/| | | | | _ < / _` | __| / __| / _ \/\ | ___/ _` |/ _` |/ _ \ __ |/ _ \ | '_ \ / _ \ '__| / _ \/\ | |\/| |/ _` | '_ \| '_ \ / _ \ '__|
6+
| | | | |_| | |_) | (_| | |_| \__ \ | (_> < | | | (_| | (_| | __/ | | | __/ | |_) | __/ | | (_> < | | | | (_| | |_) | |_) | __/ |
7+
|_| |_|\__, |____/ \__,_|\__|_|___/ \___/\/ |_| \__,_|\__, |\___|_| |_|\___|_| .__/ \___|_| \___/\/ |_| |_|\__,_| .__/| .__/ \___|_|
8+
__/ | __/ | | | | | | |
9+
|___/ |___/ |_| |_| |_|

0 commit comments

Comments
 (0)