yml mysql druid_SpringBoot在yml配置文件中配置druid的操作

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

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:

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的值。

如:有如下配置:

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中有如下配置:

info:

user:

name: zhangsan

age: 14

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

class user{

string name;

int age;

//getter 及 setter方法

}

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

@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对象:

@component

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

@configurationproperties(prefix = "info")

public class ymlconfig{

map user = new hashmap<>();

//user getter及setter方法

}

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

指定任意层

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

@component

@configurationproperties(prefix = "spring.datasource")

public class ymlconfig{

map> druid = new hashmap<>();

//user getter及setter方法

}

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

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

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

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值