亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Boot使用和配置Druid

 更新時(shí)間:2017年04月25日 11:34:50   作者:瘋狂的米老鼠  
本篇文章主要介紹了Spring Boot使用和配置Druid,Druid號稱是Java語言中最好的數(shù)據(jù)庫連接池,并且能夠提供強(qiáng)大的監(jiān)控和擴(kuò)展功能

1、引入依賴包

<!--druid-->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.27</version>
</dependency>

2、配置application.properties

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/xxxxx?characterEncoding=utf8&useSSL=true&verifyServerCertificate=false
spring.datasource.username=root
spring.datasource.password=xxxxx
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置獲取連接等待超時(shí)的時(shí)間
spring.datasource.maxWait=60000
# 配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打開PSCache,并且指定每個(gè)連接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻
spring.datasource.filters=stat,wall,log4j
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
#spring.datasource.useGlobalDataSourceStat=true

spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#懶加載配置
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

3、目前Spring Boot中默認(rèn)支持的連接池有dbcp,dbcp2, tomcat, hikari三種連接池。 由于Druid暫時(shí)不在Spring Bootz中的直接支持,故需要進(jìn)行配置信息的定制

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
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;import java.sql.SQLException;

@Configuration
public class DruidConfiguration {
  @Value("${spring.datasource.url}")
  private String dbUrl;
  @Value("${spring.datasource.username}")
  private String username;
  @Value("${spring.datasource.password}")
  private String password;
  @Value("${spring.datasource.driver-class-name}")
  private String driverClassName;
  @Value("${spring.datasource.initialSize}")
  private int initialSize;
  @Value("${spring.datasource.minIdle}")
  private int minIdle;
  @Value("${spring.datasource.maxActive}")
  private int maxActive;
  @Value("${spring.datasource.maxWait}")
  private int maxWait;
  @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
  private int timeBetweenEvictionRunsMillis;
  @Value("${spring.datasource.minEvictableIdleTimeMillis}")
  private int minEvictableIdleTimeMillis;
  @Value("${spring.datasource.validationQuery}")
  private String validationQuery;
  @Value("${spring.datasource.testWhileIdle}")
  private boolean testWhileIdle;
  @Value("${spring.datasource.testOnBorrow}")
  private boolean testOnBorrow;
  @Value("${spring.datasource.testOnReturn}")
  private boolean testOnReturn;
  @Value("${spring.datasource.poolPreparedStatements}")
  private boolean poolPreparedStatements;
  @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
  private int maxPoolPreparedStatementPerConnectionSize;
  @Value("${spring.datasource.filters}")
  private String filters;
  @Value("{spring.datasource.connectionProperties}")
  private String connectionProperties;

  @Bean   //聲明其為Bean實(shí)例
  @Primary //在同樣的DataSource中,首先使用被標(biāo)注的DataSource
  public DataSource dataSource(){
    DruidDataSource datasource = new DruidDataSource();
    datasource.setUrl(this.dbUrl);
    datasource.setUsername(username);
    datasource.setPassword(password);
    datasource.setDriverClassName(driverClassName);

    //configuration
    datasource.setInitialSize(initialSize);
    datasource.setMinIdle(minIdle);
    datasource.setMaxActive(maxActive);
    datasource.setMaxWait(maxWait);
    datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    datasource.setValidationQuery(validationQuery);
    datasource.setTestWhileIdle(testWhileIdle);
    datasource.setTestOnBorrow(testOnBorrow);
    datasource.setTestOnReturn(testOnReturn);
    datasource.setPoolPreparedStatements(poolPreparedStatements);
    datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
    try {
      datasource.setFilters(filters);
    } catch (SQLException e) {
      System.err.println("druid configuration initialization filter: "+ e);
    }
    datasource.setConnectionProperties(connectionProperties);
    return datasource;
  }

  @Bean
  public ServletRegistrationBean statViewServle(){
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
    // IP白名單
    servletRegistrationBean.addInitParameter("allow","192.168.1.218,127.0.0.1");
    // IP黑名單(共同存在時(shí),deny優(yōu)先于allow)
    servletRegistrationBean.addInitParameter("deny","192.168.1.100");
    //控制臺管理用戶
    servletRegistrationBean.addInitParameter("loginUsername","druid");
    servletRegistrationBean.addInitParameter("loginPassword","761341");
    //是否能夠重置數(shù)據(jù)
    servletRegistrationBean.addInitParameter("resetEnable","false");
    return servletRegistrationBean;
  }

  @Bean
  public FilterRegistrationBean statFilter(){
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
    //添加過濾規(guī)則
    filterRegistrationBean.addUrlPatterns("/*");
    //忽略過濾的格式
    filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    return filterRegistrationBean;
  }
}


4、瀏覽器輸入http://localhost:8081/druid/index.html


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 關(guān)于SpringBoot2.7.6連接nacos遇到的一些問題

    關(guān)于SpringBoot2.7.6連接nacos遇到的一些問題

    這篇文章主要介紹了關(guān)于SpringBoot2.7.6連接nacos遇到的一些問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能

    Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能

    在早期的網(wǎng)絡(luò)編程中,Socket是很常見的實(shí)現(xiàn)技術(shù)之一,比如早期的聊天室,就是基于這種技術(shù)進(jìn)行實(shí)現(xiàn)的,另外現(xiàn)在有些消息推送,也可以基于Socket實(shí)現(xiàn),本文小編給大家介紹了Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能的示例,需要的朋友可以參考下
    2023-11-11
  • 詳解Java適配器模式

    詳解Java適配器模式

    這篇文章主要介紹了Java適配器模式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Java的反射機(jī)制一起來看看

    Java的反射機(jī)制一起來看看

    這篇文章主要為大家詳細(xì)介紹了Java反射機(jī)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Springboot?異步任務(wù)和定時(shí)任務(wù)的異步處理

    Springboot?異步任務(wù)和定時(shí)任務(wù)的異步處理

    本文介紹了Springboot異步任務(wù)和定時(shí)任務(wù)的異步處理,Springboot?中,異步任務(wù)和定時(shí)任務(wù)是經(jīng)常遇到的處理問題方式,為了能夠用好這兩項(xiàng)配置,不干擾正常的業(yè)務(wù),需要對其進(jìn)行異步化配置。怎么設(shè)置合理的異步處理線程就是其核心和關(guān)鍵,下文詳情需要的朋友可以參考下
    2022-05-05
  • SpringBoot開啟異步調(diào)用方法

    SpringBoot開啟異步調(diào)用方法

    這篇文章主要為大家詳細(xì)介紹了SpringBoot開啟異步調(diào)用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • 在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云

    在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云

    這篇文章主要介紹了在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • Flink作業(yè)Task運(yùn)行源碼解析

    Flink作業(yè)Task運(yùn)行源碼解析

    這篇文章主要為大家介紹了Flink作業(yè)Task運(yùn)行源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • SpringMVC @ControllerAdvice使用場景

    SpringMVC @ControllerAdvice使用場景

    這篇文章主要介紹了SpringMVC @ControllerAdvice使用場景,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 利用Java實(shí)現(xiàn)在PDF中添加工具提示

    利用Java實(shí)現(xiàn)在PDF中添加工具提示

    這篇文章主要介紹了如何通過Java在PDF中添加工具提示,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定的參考價(jià)值,感興趣的可以學(xué)習(xí)一下
    2022-01-01

最新評論