Spring Boot Configuration Druid

Source: Internet
Author: User
Tags connection pooling

1. To introduce dependencies, all dependencies are based on the previous spring Boot+mybatis dependency, plus the following dependencies, as follows:

<!-- Druid数据库连接池组件 --><dependency>    <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version></dependency>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

Note that because the Druid configuration also requires some annotations, such as @WebInitParam @WebFilter , they come from the Tomcat-embed-core package in spring boot, and the package comes from Spring-boot-starter-web, so In addition to the above dependencies, it is important to ensure that spring-boot-starter-web the introduction of my project into the specific following:

 <!--Spring Boot Web Dependency--<Dependency><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-web</Artifactid><Exclusions><Exclusion><Groupid>org.slf4j</Groupid><Artifactid>slf4j-log4j12</artifactid> </ exclusion> </exclusions> <dependency> < groupid>com.alibaba</groupId > <artifactid>druid</artifactid> <version>1.0.18</version> </ dependency> </dependency>  
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

2. Add the following to the profile file in the Spring boot Project application.properties :

# #数据库连接信息spring. DataSource. url=jdbc:oracle:thin:@//127.0.0.1:1521/orclspring. DataSource. username=nqsdspring. DataSource. password=nqsdspring. DataSource. driver-class-name=oracle. jdbc. Driver. Oracledriver################## #以下为druid增加的配置 ########################## #spring. DataSource. type=Com. Alibaba. Druid. Pool. Druiddatasource# The following supplemental settings for connection pooling are applied to all of the above data sources# Initialize size, min, Max Spring. DataSource. initialsize=5spring. DataSource. minidle=5spring. DataSource. maxactive=20# Configure the time to get the connection wait timeout spring. DataSource. maxwait=60000# How long does the configuration interval take to detect idle connections that need to be closed, in milliseconds spring. DataSource. timebetweenevictionrunsmillis=60000# Configure a connection in the pool the minimum time to live, in milliseconds spring. DataSource. minevictableidletimemillis=300000spring. DataSource. validationquery=select1 from Dualspring. DataSource. testwhileidle=truespring. DataSource. testonborrow=falsespring. DataSource. testonreturn=false# Open Pscache, and specify the size of Pscache on each connection spring. DataSource. poolpreparedstatements=truespring.datasource20# Configure Monitoring statistics interception filters, remove the post-monitoring interface SQL cannot be counted, ' wall ' for firewalls Spring.datasource.filters=stat,wall,log4j# with connectproperties properties to open Mergesql function ; Slow SQL Records Spring.datasource.connectionproperties= Druid.stat.mergesql=true ;d ruid.stat.slowsqlmillis=5000# Merging monitoring data for multiple Druiddatasource  #spring. Datasource.useglobaldatasourcestat=true############## #以上为配置druid添加的配置 ########################################   
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

?? Druid itself is to extend the functionality of the JDBC, and the DataSource object is the configuration of JDBC, so the above more of those datasource configuration, also can understand, specifically what features I have not seen.

3. You also need to add 3 configuration classes, as follows:

Package microservice.qssj.config;Import Javax.sql.DataSource;Import org.springframework.boot.context.properties.ConfigurationProperties;Import Org.springframework.context.annotation.Bean;Import org.springframework.context.annotation.Configuration;import Com.alibaba.druid.pool.DruidDataSource; /** * Configure Druid Required configuration class, introduce Application.properties file to start with spring.datasource information * Therefore, you need to configure the relevant information in the Application.properties file. * @author Administrator * */ @Configuration public class  druidconfig { @Bean  @ConfigurationProperties (prefix = Span class= "hljs-string" > "Spring.datasource") public DataSource druiddatasource () {Druiddatasource Druiddatasource = new Druiddatasource (); return Druiddatasource;} }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

?? You can see that only the DataSource object's implementation class becomes an DruidDataSource object.
?? Is the configuration of the filter rule.

Package microservice.qssj.config;Import Javax.servlet.annotation.WebFilter;import Javax.servlet.annotation.WebInitParam; import com.alibaba.druid.support.http.WebStatFilter; /** * Configure Druid filter rules * @author Administrator * */ @WebFilter (Filtername=" Druidwebstatfilter ", Urlpatterns="/* ", initparams={ @WebInitParam (Name=" *.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/* ") //Ignore resource}" public  Class druidstatfilter extends  Webstatfilter {}               
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

?? is to configure the Druid page login password, and black and white list settings.

Package microservice.qssj.config;Import Javax.servlet.annotation.WebInitParam;Import Javax.servlet.annotation.WebServlet;Import Com.alibaba.druid.support.http.StatViewServlet;/** * Configuring Druid Page Configuration *@author Administrator * * * *@SuppressWarnings ("Serial")@WebServlet (Urlpatterns ="/druid/*", initparams={@WebInitParam (name="Allow", value="192.168.1.20,127.0.0.1"),IP whitelist (no configuration or null, all access is allowed)@WebInitParam (name="Deny", value= "192.168.16.111"), //IP blacklist (when common, deny takes precedence over allow)  @WebInitParam (Name=" Loginusername ", Value= "Admin"), //user name  @WebInitParam (name=  "Loginpassword", Value= "admin",  Password  @WebInitParam (Name= "resetenable", Value=//disable "Reset all" on HTML page) public Span class= "Hljs-class" >class druidstatviewservlet extends statviewservlet {}       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

?? Once the above three classes have been configured, you need to add one more annotation to the startup class, i.e.@ServletComponentScan

Package MICROSERVICE.QSSJ;Import Org.mybatis.spring.annotation.MapperScan;Import org.springframework.boot.SpringApplication;Import org.springframework.boot.autoconfigure.SpringBootApplication;Import Org.springframework.boot.web.servlet.ServletComponentScan;Import Org.springframework.context.annotation.ImportResource;Import org.springframework.transaction.annotation.EnableTransactionManagement;/** * Ownership start class * @author Administrator */ @SpringBootApplication  @ServletComponentScan //configuration druid must add annotations, if not added, access to the page can not open, filter and servlet, listener and the like need to register separately to use, spring Boot provides this annotation as a registration function  @MapperScan ( " Microservice.qssj.mapper ") //must add this, without error, if not added, you can also add mapper comments on each @mapper, //here also to add a general note, specifically forgot public  class qssjserviceapplication { Public static void main (String[] args) {Springapplication.run (qssjserviceapplication.class, args);}}         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st

?? Note the newly added three configuration class locations, where they put the package structure lower than the startup class.
Startup class Location:??? package microservice.qssj;
Druid Configuration class Location:?package microservice.qssj.config;

4. After the above configuration, after starting the project, you can access the Druid page, such as my project port number is 30001, my access page is as follows: (I do not configure the project name, the default spring boot page from "/" start)

?? The account password is the admin configured in the configuration class.


?? If you have questions about the source of this page, because we configured the process and did not configure the index.html page, I estimate it should be in the introduction of the Druid Jar package, specific unknown I have not looked for.
?? Access methods are fixed/druid/index.html or/druid/login.html

Spring Boot Configuration Druid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.