好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringBoot在yml配置文件中配置druid的操作

最新版的druid和旧版在filter配置方面有些不同,以下是旧版druid中配置filter:

?

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

spring:

  ##数据库连接信息

  datasource:

  url: jdbc:mysql: //localhost:3306/young

  username: root

  password: root

  driver- class -name: com.mysql.jdbc.Driver

  ###################以下为druid增加的配置###########################

  type: com.alibaba.druid.pool.DruidDataSource

  # 下面为连接池的补充设置,应用到上面所有数据源中

  # 初始化大小,最小,最大

  initialSize: 5

  minIdle: 5

  maxActive: 20

  # 配置获取连接等待超时的时间

  maxWait: 60000

  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

  timeBetweenEvictionRunsMillis: 60000

  # 配置一个连接在池中最小生存的时间,单位是毫秒

  minEvictableIdleTimeMillis: 300000

  validationQuery: SELECT 1 FROM DUAL

  testWhileIdle: true

  testOnBorrow: false

  testOnReturn: false

  # 打开PSCache,并且指定每个连接上PSCache的大小

  poolPreparedStatements: true

  maxPoolPreparedStatementPerConnectionSize: 20

  # 配置监控统计拦截的filters,去掉后监控界面sql无法统计, 'wall' 用于防火墙

  filters: stat,wall,log4j

  # 通过connectProperties属性来打开mergeSql功能;慢SQL记录

  connectionProperties: druid.stat.mergeSql= true ;druid.stat.slowSqlMillis= 5000

  # 合并多个DruidDataSource的监控数据

  useGlobalDataSourceStat: true

  ###############以上为配置druid添加的配置###########################

下面是1.1.10版本的druid配置filter:

?

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

spring:

  ##数据库连接信息

  datasource:

  url: jdbc:mysql: //localhost:3306/day05

  username: root

  password: 15963asd

  driver- class -name: com.mysql.jdbc.Driver

  ###################以下为druid增加的配置###########################

  type: com.alibaba.druid.pool.DruidDataSource

  # 下面为连接池的补充设置,应用到上面所有数据源中

  # 初始化大小,最小,最大

  initialSize: 5

  minIdle: 5

  maxActive: 20

  # 配置获取连接等待超时的时间

  maxWait: 60000

  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒

  timeBetweenEvictionRunsMillis: 60000

  # 配置一个连接在池中最小生存的时间,单位是毫秒

  minEvictableIdleTimeMillis: 300000

  validationQuery: SELECT 1 FROM DUAL

  testWhileIdle: true

  testOnBorrow: false

  testOnReturn: false

  # 打开PSCache,并且指定每个连接上PSCache的大小

  poolPreparedStatements: true

  maxPoolPreparedStatementPerConnectionSize: 20

  # 配置监控统计拦截的filters,去掉后监控界面sql无法统计, 'wall' 用于防火墙,此处是filter修改的地方

  filters:

   commons-log.connection-logger-name: stat,wall,log4j

  # 通过connectProperties属性来打开mergeSql功能;慢SQL记录

  connectionProperties: druid.stat.mergeSql= true ;druid.stat.slowSqlMillis= 5000

  # 合并多个DruidDataSource的监控数据

  useGlobalDataSourceStat: true

顺便附一下出现在springboot中yml配置文件里面配置druid的filter配置错误的信息:

Property: spring.datasource.filters

Value: stat,wall,log4j

Origin: class path resource [application.yml]:29:14

Reason: Unable to set value for property filters

补充知识: Springboot中yml文件读取

SpringBoot的.yml文件是一个非常简洁明了的配置文件,可看作.properties的精简版。

一般来讲,我们通过@Value这个注解就可以直接获取到某个properties的值。

如:有如下配置:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

spring:

  datasource:

   druid:

    localhost:

     driverClassName: com.mysql.jdbc.Driver

     url: jdbc:mysql: //localhost:3306/paas-dashboard?useUnicode=true&characterEncoding=utf8

     username: root

     password: 123

     master:

     driverClassName: oracle.jdbc.OracleDriver

     url: jdbc:oracle:thin:@ //172.21.0.73:1621/tthradb

     username: dbchnesbcfgcha

     password: dbchnesbcfgcha

一般来讲,直接通过@Value(spring.datasource.druid.localhost.driverClassName)直接获取到这个值了。

但是如果需要直接获取到localhost下面所有的配置呢?或者自己指定某一层下面所有的配置信息呢?

简单示例

SpringBoot中还有一种非常强大的注解@ConfigurationProperties,使用该注解可直接将yml的配置直接注入到某个对象中。

如:yml中有如下配置:

?

1

2

3

4

info:

  user:

   name: zhangsan

   age: 14

这时,我们定义个User对象:

?

1

2

3

4

5

6

class User{

  String name;

  int age;

 

//getter 及 setter方法

}

在Spring容器中直接通过@ConfigurationProperties来注入,需要指定前缀到配置文件中user的上一层。对象名必须同yml中的配置。

?

1

2

3

4

5

6

7

8

@Component

@PropertySource ( "classpath:application-druid.yml" ) //指定yml文件位置

@ConfigurationProperties (prefix = "info" )

public class YmlConfig{

 

  User user = new User();

//user getter及setter方法

}

Spring容器启动后,yml中的配置的属性即注入到user对象。

或者我们也可以用个Map来进行封装,配置文件中的属性无非就是key:value的形式,同样定义user对象:

?

1

2

3

4

5

6

7

8

@Component

@PropertySource ( "classpath:application-druid.yml" ) //指定yml文件位置

@ConfigurationProperties (prefix = "info" ) 

public class YmlConfig{

 

  Map<String,String> user = new HashMap<>();

//user getter及setter方法

}

同样也能注入到user的Map对象。

指定任意层

如本文开始的那个yml配置文件的配置,如果,我想直接获取到所有的数据源的配置,那么就必须要指定一个对象能装下所有的这些配置,可以自定义对象,或者直接使用Map。如,我们定义如下的Map:

?

1

2

3

4

5

6

7

@Component

@ConfigurationProperties (prefix = "spring.datasource" )

public class YmlConfig{

 

  Map<String,Map<String,String>> druid = new HashMap<>();

//user getter及setter方法

}

Spring容器其中后,配置文件中spring.datasource.druid以下的配置属性同样能注入到druid对象中去。

同样指定其他层的配置,只要符合某个对象的数据结构,就能将配置的属性注入到该对象中去。

以上这篇SpringBoot在yml配置文件中配置druid的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/JAdroid/article/details/80490679

查看更多关于SpringBoot在yml配置文件中配置druid的操作的详细内容...

  阅读:39次