1. 程式人生 > 程式設計 >SpringBoot在yml配置檔案中配置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,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,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<String,String> user = new HashMap<>();
//user getter及setter方法
}

同樣也能注入到user的Map物件。

指定任意層

如本文開始的那個yml配置檔案的配置,如果,我想直接獲取到所有的資料來源的配置,那麼就必須要指定一個物件能裝下所有的這些配置,可以自定義物件,或者直接使用Map。如,我們定義如下的Map:

@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的操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。