上篇是一种配置方式,虽然我们创建了servlet、filter但是没有任务编码,看着是不是很不爽。ok,接下来说一下简介的配置方式,使用代码注册Servlet,也是我个人比较推荐的!

1、创建DruidConfiguration.java类

package com.rick.common.conf;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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; /**
* Desc : Druid数据源配置,这种方式不需要创建servlet、filter,
* 不需要增加注解@ServletComponentScan
* User : RICK
* Time : 2017/8/22 10:15
*/ @Configuration
public class DruidConfiguration {
/**
* 注册一个StatViewServlet
*
* @return
*/
@Bean
public ServletRegistrationBean DruidStatViewServle2() {
//org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid2/*");
//添加初始化参数:initParams
//白名单:
servletRegistrationBean.addInitParameter("allow", "192.168.2.205,127.0.0.1");
//IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
servletRegistrationBean.addInitParameter("deny", "192.168.2.100");
//登录查看信息的账号密码.
servletRegistrationBean.addInitParameter("loginUsername", "anna");
servletRegistrationBean.addInitParameter("loginPassword", "anna");
//是否能够重置数据.
servletRegistrationBean.addInitParameter("resetEnable", "false");
return servletRegistrationBean;
} /**
* 注册一个:filterRegistrationBean
*
* @return
*/
@Bean
public FilterRegistrationBean druidStatFilter2() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
//添加过滤规则.
filterRegistrationBean.addUrlPatterns("/*");
//添加不需要忽略的格式信息.
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*");
return filterRegistrationBean;
}
}

2、启动项目,访问http://localhost:8080/druid2/login.html效果和第一方式一样的,在这就不做截图了

3、项目清单

170629、springboot编程之Druid数据源和监控配置二的更多相关文章

  1. 170628、springboot编程之Druid数据源和监控配置一

    Spring Boot默认的数据源是:org.apache.tomcat.jdbc.pool.DataSource,那么如何修改数据源呢?我已目前使用比较多的阿里数据源Druid为例,如果使用其他的数 ...

  2. spring boot配置druid数据源和监控配置

    直接上代码: 一.pom.xml中添加依赖 <dependency> <groupId>com.github.drtrang</groupId> <artif ...

  3. springboot学习笔记-4 整合Druid数据源和使用@Cache简化redis配置

    一.整合Druid数据源 Druid是一个关系型数据库连接池,是阿里巴巴的一个开源项目,Druid在监控,可扩展性,稳定性和性能方面具有比较明显的优势.通过Druid提供的监控功能,可以实时观察数据库 ...

  4. SpringBoot:Mybatis + Druid 数据访问

    西部开源-秦疆老师:基于SpringBoot 2.1.7 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! 简介 对于数据访问层 ...

  5. 阿里druid连接池监控配置

    首先在web.xml中添加如下配置: <filter> <filter-name>DruidWebStatFilter</filter-name> <filt ...

  6. SpringBoot项目整合Druid进行统计监控

    0.druid介绍,参考官网 1.在项目的POM文件中添加alibaba的druid依赖 <dependency> <groupId>com.alibaba</group ...

  7. 170717、springboot编程之mybatis数据库开发和aop拦截

    一.springboot整合mybaits (1)新建maven project; 新建一个maven project,取名为:spring-boot-mybatis (2)在pom.xml文件中引入 ...

  8. 170622、springboot编程之JPA操作数据库

    JPA操作数据库 什么事JAP?JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 1.在pom ...

  9. 170720、springboot编程之properties文件讲解

    但是在实际开发过程中有更复杂的需求,我们在对properties进一步的升华.在本篇博客中您将会学到如下知识(这节中有对之前的知识的温故,对之前的升华): (1) 在application.prope ...

随机推荐

  1. thinkphp 使用原生mysql语句 联合查询

    <?php class DelAction extends Action { public function ml(){ // 实例化一个空模型,没有对应任何数据表 $Dao = M(); // ...

  2. ubuntu之Matlab安装

    (清华大学校内适用) 1.首先下载Matlab镜像:http://its.tsinghua.edu.cn/column/jsrj/1,一共有两个ios文件. 2.然后执行 cd ~ mkdir mat ...

  3. e684. 以多种格式打印

    A Book object is used when printing pages with different page formats. This example prints the first ...

  4. error: expected declaration specifiers or '...' before 'xxxx'(xxxx是函数形参)

    今天汗颜了一大阵 早上,在编译我的源代码的时候竟然不通过编译,上个星期六也出现了这种情况,当时不知道怎么弄的后来又通过编译了,可能是原来的.o文件没有make clean 还保存在那里,以至于蒙过去了 ...

  5. IOS 命令行编译

    转自:简书 IOS 命令行编译   发表于 IOS2013-08-17 07:07 字数: 583 阅读量: 61 This document will note about the ios comm ...

  6. SpringMVC-Controller

    接上: web.xml Spring-servlet.xml Controller层是控制层,在其类上添加@Controller注解,会被Spring-servlet.xml中的<context ...

  7. Bash 脚本 getopts为什么最后一个參数取不到

    看以下的Bash脚本: #!/bin/bash interval=0 count=0 pid="" while getopts "p:d:n" arg do c ...

  8. c++ const enum #define

    最后的最后,我们略微总结一下:        1.只是声明单一固定值,尽可能采用const.        2.如果是一组固定值,并且互相有关联,则采用enum.        3.不涉及条件编译,只 ...

  9. SRCNN之后的深度学习超分辨率

    SRCNN开山之作 IDN 信息蒸馏网络information distillation network(IDN) Fast and Accurate Single Image Super-Resol ...

  10. JavaScript匿名函数和回调函数

    匿名函数的自调函数格式: (function(){ //代码 })(); <script type="text/javascript"> (function(){ al ...