1. 通过@AutoLoad方式实现自己的Druid Filter

比如,Cat这个监控:

package com.wlqq.loan.rule.config;import com.alibaba.druid.filter.AutoLoad;
import com.alibaba.druid.filter.FilterAdapter;
import com.alibaba.druid.filter.FilterChain;
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
import com.alibaba.druid.proxy.jdbc.StatementProxy;
import com.dianping.cat.Cat;
import com.dianping.cat.CatConstants;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;import java.sql.SQLException;/*** cat监控drui连接池** @author flowkr90@gmail.com*/
@AutoLoad
public class DruidCatFilter extends FilterAdapter {private String dbType;private String url;@Overridepublic void init(DataSourceProxy dataSource) {dbType = dataSource.getDbType();url = dataSource.getUrl();}@Overridepublic boolean statement_execute(FilterChain chain, StatementProxy statement, String sql) throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);boolean result;try {result = super.statement_execute(chain, statement, sql);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, int autoGeneratedKeys)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);boolean result;try {result = super.statement_execute(chain, statement, sql, autoGeneratedKeys);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, int[] columnIndexes)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);boolean result;try {result = super.statement_execute(chain, statement, sql, columnIndexes);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, String[] columnNames)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);boolean result;try {result = super.statement_execute(chain, statement, sql, columnNames);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic int[] statement_executeBatch(FilterChain chain, StatementProxy statement) throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", statement.getBatchSql());int[] result;try {result = super.statement_executeBatch(chain, statement);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);ResultSetProxy result;try {result = super.statement_executeQuery(chain, statement, sql);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql) throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);int result;try {result = super.statement_executeUpdate(chain, statement, sql);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, int autoGeneratedKeys)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);int result;try {result = super.statement_executeUpdate(chain, statement, sql, autoGeneratedKeys);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, int[] columnIndexes)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);int result;try {result = super.statement_executeUpdate(chain, statement, sql, columnIndexes);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, String[] columnNames)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", sql);int result;try {result = super.statement_executeUpdate(chain, statement, sql, columnNames);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic boolean preparedStatement_execute(FilterChain chain, PreparedStatementProxy statement) throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", statement.getSql());boolean result;try {result = super.preparedStatement_execute(chain, statement);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic ResultSetProxy preparedStatement_executeQuery(FilterChain chain, PreparedStatementProxy statement)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", statement.getSql());ResultSetProxy result;try {result = super.preparedStatement_executeQuery(chain, statement);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}@Overridepublic int preparedStatement_executeUpdate(FilterChain chain, PreparedStatementProxy statement)throws SQLException {Transaction transaction = Cat.newTransaction(CatConstants.TYPE_SQL, url);Cat.logEvent("SQL.DbType", dbType);Cat.logEvent("SQL.Database", url);Cat.logEvent("SQL.sql", statement.getSql());int result;try {result = super.preparedStatement_executeUpdate(chain, statement);transaction.setStatus(Message.SUCCESS);} catch (SQLException e) {Cat.logError(e);transaction.setStatus(e);throw e;} finally {transaction.complete();}return result;}}

配置SPI

src/main/resources/META-INF/services 目录下创建 com.alibaba.druid.filter.Filter这个文件

文件内容为 filter 的全路径

com.wlqq.loan.rule.config.DruidCatFilter

通过SPI方式定制druid filter相关推荐

  1. niosii spi 外部_NIOS II SPI详解 如何使用SPI方式传输

    NIOS II SPI 详解 如何使用 SPI 方式传输 1 .说明 本文是依据笔者阅读< Embedded Peripherals (ver 9.0, Mar 2009, 4 MB).pdf ...

  2. 台式机安装EXSI,通过官方方式定制安装包

    台式机安装EXSI,通过官方方式定制安装包 需要使用第三方工具 ESXi-Customizer-PS 对安装镜像进行修改,加载我们想要的驱动. 下载和安装 PowerShell: https://do ...

  3. STM32使用SPI方式读写SD 卡

    掌握SD卡协议原理,用STM32F103 完成对SD卡的数据读取(FAT文件模式). 文章目录 一.前言 1.SD卡 2.FATFS 二.工程分析 1.代码分析 2.连线 3.编译工程并烧录 4.验证 ...

  4. ② ESP8266 开发学习笔记_By_GYC 【ESP8266 驱动 ws2812 三原色灯(spi方式 稳定灯光)】

    目录 ② ESP8266 开发学习笔记_By_GYC [ESP8266 驱动 ws2812 三原色灯(spi方式 稳定灯光)] 一.驱动ws2812遇到的问题 二.可能的方案 三.具体实现 四.测试程 ...

  5. stm32 4线SPI方式驱动CH452A数码管驱动芯片

    stm32 采用的是4线SPI方式连接.操作命令是12位的数据,采用的是io模拟的方式驱动.试过使用硬件SPI驱动,但是命令只支持12位,硬件SPI发送的是8位或者16位,是驱动不起来的. 常用指令如 ...

  6. 基于GCCAVR的TLC2543读写程序----模拟SPI方式实现

    TLC2453的操作也比较简单,通过模拟的方式实现只需要看一下时序图把时序搞对即可. 在硬件SPI中,读和写实可以通过硬件同步进行的,因此在写入本次命令字时可以同时读取上一次命令的数据,在模拟SPI的 ...

  7. 基于SPI方式实现OLED屏显

    文章目录 一.SPI简介 1.1物理层 1.2协议层 二.OLED 2.1定义 2.2优势 2.3模块工作模式选择 2.4模块特点 三.实验过程 3.1实验准备 3.2硬件连接 3.3程序烧录 3.3 ...

  8. 基于Xilinx平台MicroBlaze的SPI方式FatFs移植

    FatFs是面向小型嵌入式系统的一种通用的FAT文件系统.它完全是由AISI C语言编写并且完全独立于底层的I/O介质.因此它可以很容易地不加修改地移植到其他的处理器当中,如8051.PIC.AVR. ...

  9. SPI方式读写SD卡速度有多快?

    很久没有写公众号了,一方面忙,另一方面也不知道写些什么内容,大家如果有想了解的(前提是我也懂),可以后台发送给我. 今天主要来测试一下SPI读写SD卡的速度.SD卡是一个嵌入式中非常常用的外设,可以用 ...

最新文章

  1. wait/notify/notifyAll在Object类中
  2. 对软件测试的理解(英文)
  3. matplotlib 雷达图2
  4. Linux C 数据结构---线性表
  5. Java不是true值不变_Java语言中String a=a;String b=a; 为什么 a==b 值为 true?
  6. Swift中的数据存储
  7. python扫雷代码_没有Python不能做的游戏,这些都可以做(附赠大型游戏开发源码)...
  8. wubi安裝ubuntukylin 14.04过程以及基本配置
  9. 使用继电器制作振荡器
  10. 哲学家就餐问题python解决_哲学家进餐问题-3中解决方案
  11. Springcloud电子商城系统 java B2B2C-服务消费者(rest+ribbon)
  12. Windows更新错误
  13. 论文参考文献尾注引用方法
  14. 整形美容的消费者心理分析
  15. 24 个很酷的 Linux/Unix 工具
  16. p50 p51 p52 p53
  17. 2019 年度程序员薪酬报告:40 岁以后普遍遭遇收入天花板
  18. 关于PEAP认证的过程说明
  19. 教授专栏08| 徐岩:青年震荡与青年危机
  20. 如何降低数据中心机柜的局部高热

热门文章

  1. hge source explor 0x9 Power Random Ini
  2. excel做地图热力图_使用Excel 2016的PowerMap模块绘制可视化地图
  3. Divisibility Problem
  4. 一打开控制台窗口就中文输入_Lightconverse灯光设计软件中文电子文档教程免费分享...
  5. 腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
  6. CoppeliaSim(原V-REP)教育版不给下载的解决方法
  7. .net C#反编译及脱壳常用工具--小结
  8. java毕业设计乐勤网书店源码+lw文档+mybatis+系统+mysql数据库+调试
  9. ASV并行断言中时序的描述sequence
  10. 四大微博开始对新用户进行实名注册