application.properties

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
######################数据库访问配置#########################
# 数据库链接
spring.datasource.url=jdbc:mysql://localhost:3306/coder4j?useUnicode=true&characterEncoding=utf-8&useSSL=false
# 数据库用户名
spring.datasource.username=root
# 数据库用户密码
spring.datasource.password=root
# 数据库驱动类
# spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mysql driver 6.0以后要使用下面的类
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 主数据源(这里使用的druid),如果是通过代码注入,这里没什么用
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
######################数据库连接池配置#########################
# sb1.3以前这样配置是可以直接注入的,1.4之后只支持几种内置的,所以我们要通过
# 代码手动注入,代码配置优先级比配置文件的高
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true

注意:spring boot 1.4以前版本,这些配置已经够了,spring boot会自动注入,但是之后的版本,只有几种内置的数据源可以通过配置注入,druid暂时没有内置,所以要通过代码注入

DruidConfig.java

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
/**
* Copyright (c) 2017, 791650277@qq.com(Mr.kiwi) All Rights Reserved.
*/
package cn.coder4j.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
* 项目:jalfred
* 包名:cn.coder4j
* 功能:数据源druid的配置
* 时间:2017-02-17 23:30
* 作者:Mr.Kiwi
*/
@Configuration
// Primary同名bean,会以些为准
@Primary
public class DataSourceConfig {

@Bean
// ConfigurationProperties会注入同名属性
@ConfigurationProperties("spring.datasource")
public DataSource druidDataSource(){

DruidDataSource druidDataSource = new DruidDataSource();

return druidDataSource;
}


/**
* 注册一个StatViewServlet
* durid统计页面相关的servlet
* @return
*/
@Bean
public ServletRegistrationBean druidStatViewServle(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid2/*");

//添加初始化参数:initParams

//白名单:
servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
servletRegistrationBean.addInitParameter("deny","192.168.1.73");
//登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername","admin2");
servletRegistrationBean.addInitParameter("loginPassword","123456");
//是否能够重置数据.
servletRegistrationBean.addInitParameter("resetEnable","false");
return servletRegistrationBean;
}

/**
* 注册一个:filterRegistrationBean
* 拦截要被druid统计的页面
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter(){

FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());

//添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");

//添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}

}

本文地址 https://blog.coder4j.cn/posts/f23e3ef2/