druid源码解读--filter-chain设计模式

filter-chain设计模式

if (filters.size() > 0) {
   
            FilterChainImpl filterChain = new FilterChainImpl(this);
            return filterChain.dataSource_connect(this, maxWaitMillis);
        }

if (this.pos < filterSize) {
   
            return nextFilter()
                    .connection_connect(this, info);
        }

filter-chain设计模式的类或接口主要有一下几个Filter、FilterAdapter、FilterChain、FilterChainImpl、FilterEventAdapter、FilterManager 以及两个具体的filter:StatFilter、LogFilter

在这里插入图片描述

Filter接口

定义了过滤器的功能,入参是FilterChain

ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException;

FilterChain接口

定义了这个过滤器链的事件方法。

ConnectionProxy connection_connect(Properties info) throws SQLException;

FilterChainImpl实现类

1、具体处理chain中事件的具体事件;

2、将请求分发给下一个filter‘“处理” (pos位置)

public ConnectionProxy connection_connect(Properties info) throws SQLException {
   
    if (this.pos < filterSize) {
   
        return nextFilter()
                .connection_connect(this, info);
    }

    Driver driver = dataSource.getRawDriver();
    String url = dataSource.getRawJdbcUrl();

    Connection nativeConnection = driver.connect(url, info);

    if (nativeConnection == null) {
   
        return null;
    }

    return new ConnectionProxyImpl(dataSource, nativeConnection, info, dataSource.createConnectionId());
}

FilterEventAdapter类

将我们关注的具体事件,通过适配器的设计模式,分为doBefore,do,doAfter三种事件

public ConnectionProxy connection_connect(FilterChain chai
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值