【Java】Springboot集成Druid
  Springboot集成Druid⽅案:⼀个是在POM中直接配置druid-spring-boot-starter,不⽤写任何代码;⼀个是配置druid,写⼏⾏代码,可以加⼊;在⽅案⼀基础上加⼊stat、wall后进⾏验证登录。
  测试环境是:SpringBoot2.1.4.RELEASE。
⼀、直接使⽤druid-spring-boot-starter
1、POM配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--⾃启动Druid管理后台-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
2、application.properties配置
### db config ###
pe=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/XXX?useUnicode=true&characterEncoding=utf8¤tSchema=public
spring.datasource.username=postgres
spring.datasource.password=XXX
spring.jpa.properties.hibernate._contextual_creation=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
#config druid
#连接池的设置
#初始化时建⽴物理连接的个数
spring.datasource.druid.initial-size=5
#最⼩连接池数量
spring.datasource.druid.min-idle=5
#最⼤连接池数量 maxIdle已经不再使⽤
spring.datasource.druid.max-active=20
#获取连接时最⼤等待时间,单位毫秒
spring.datasource.druid.max-wait=60000
#申请连接的时候检测,如果空闲时间⼤于timeBetweenEvictionRunsMillis,执⾏validationQuery检测连接是否有效。
spring.st-while-idle=true
#既作为检测的间隔时间⼜作为testWhileIdel执⾏的依据
spring.datasource.druid.time-between-eviction-runs-millis=60000
#销毁线程时检测当前连接的最后活动时间和当前时间差⼤于该值时,关闭当前连接
spring.datasource.druid.min-evictable-idle-time-millis=30000
#⽤来检测连接是否有效的sql 必须是⼀个查询语句
#mysql中为select'x'
#oracle中为select1from dual
spring.datasource.druid.validation-query=select'x'
#申请连接时会执⾏validationQuery检测连接是否有效,开启会降低性能,默认为true
spring.st-on-borrow=false
#归还连接时会执⾏validationQuery检测连接是否有效,开启会降低性能,默认为true
spring.st-on-return=false
#当数据库抛出不可恢复的异常时,抛弃该连接
spring.ption-sorter=true
#是否缓存preparedStatement,mysql5.5+建议开启
#spring.datasource.druid.pool-prepared-statements=true
#当值⼤于0时poolPreparedStatements会⾃动修改为true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
#配置扩展插件
spring.datasource.druid.filters=stat,wall
#通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.tion-properties=Sql=true;druid.stat.slowSqlMillis=500
#合并多个DruidDataSource的监控数据
spring.datasource.druid.use-global-data-source-stat=true
#设置访问druid监控页的账号和密码,默认没有
#spring.datasource.druid.stat-view-servlet.login-username=admin
#spring.datasource.druid.stat-view-servlet.login-password=admin
### mybatis config ###
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
### log config ###
PS:⼴告⽆处不在
⼆、使⽤代码,可验证登录的⽅案
1、POM配置,删除:druid-spring-boot-starter
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--⾃启动Druid管理后台-->
<!--        <dependency>-->
<!--            <groupId>com.alibaba</groupId>-->
<!--            <artifactId>druid-spring-boot-starter</artifactId>-->
<!--            <version>1.1.10</version>-->
<!--        </dependency>-->
2、application.properties配置不变
3、新建⼀个类:DruidConfig,注意包的引⼊,特别是:
package com.jrsc.supervisor.druid;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.t.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import t.annotation.Bean;
import t.annotation.Configuration;
jpa mybatisimport javax.sql.DataSource;
3、DruidConfig具体实现如下:
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource getDataSource() {
return new DruidDataSource();
}
@Bean
public ServletRegistrationBean druidStatViewServlet() {
//ServletRegistrationBean提供类的进⾏注册
ServletRegistrationBean servletRegistrationBean =
new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//添加初始化参数:initParams
/
/⽩名单:
servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
//IP⿊名单(同时存在时,deny优先于allow)
//如果满⾜deny,就提⽰:sorry,you are not permitted to view this page
servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
//登录查看信息的账号和密码
servletRegistrationBean.addInitParameter("loginUsername", "admin");
servletRegistrationBean.addInitParameter("loginPassword", "123456");
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
}
@Bean
public FilterRegistrationBean druidStatFilter() {
FilterRegistrationBean filterRegistrationBean =
new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则
filterRegistrationBean.addUrlPatterns("/*");
//添加需要忽略的格式信息
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif," +
"*.jpg,*.png, *.css,*.ico,/druid/*");
return filterRegistrationBean;
}
4、对于SpringBoot的启动类,加⼊注解:@ServletComponentScan
三、通过在⽅案⼀基础上加⼊如下代码,实现验证登录
#设置访问druid监控页的账号和密码,默认没有
## Druid WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
spring.datasource.abled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.lusions=*.gif,*.png,*.jpg,*.html,*.js,*.css,*.ico,/druid/* # Druid StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
spring.datasource.abled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.set-enable=true
spring.datasource.druid.stat-view-servlet.login-username=admin
spring.datasource.druid.stat-view-servlet.login-password=123456
spring.datasource.druid.stat-view-servlet.allow=
spring.datasource.druid.stat-view-servlet.deny=
四、测试

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。