理解springboot那些过滤器与调用链、包装或封装、设计模式相关等命名规范,就可以读懂80%的springboot源代码,和其他Java框架代码

本文介绍了SpringBoot中过滤器、调用链、包装或封装以及设计模式的相关命名规范,如Pipeline、Chain、Filter、Interceptor、Evaluator、Detector等,通过这些概念能帮助读者更好地理解SpringBoot及Java框架的源代码。同时,文章讨论了缓存、组合、条件构造器等技术在实际应用中的实现和作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

紧接上面《

理解springboot那些注册与回调、监控与统计等命名规范,就可以读懂70%的springboot源代码》、《

理解springboot那些约定俗成的框架类名、全局context等命名规范,就可以读懂一半springboot的源代码》2篇文章,此片将汇总springboot那些过滤器与调用链、包装或封装、设计模式相关等命名规范,以便更加精进阅读springboot源代码

过滤器与调用链


Pipeline:

流管道,用在流数据处理中。

如:Springboot下redis用pipelining管道模式写入,使写入性能更佳!

引入pipelining,与传统模式对比

compile('redis.clients:jedis:2.9.0')  #gradle引入
compile('org.springframework.data:spring-data-redis:2.0.8.RELEASE')

测试代码:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

public class BatchOperSet {

    private static final String HOST = "127.0.0.1";
    private static final int PORT = 6379;

    // 批量插入数据到Redis,正常使用
    public static void batchSetNotUsePipeline() throws Exception {
        Jedis jedis = new Jedis(HOST, PORT);
        String keyPrefix = "normal";
        long begin = System.currentTimeMillis();
        for (int i = 1; i < 10000; i++) {
            String key = keyPrefix + "_" + i; 
            String value = String.valueOf(i);
            jedis.set(key, value);
        }
        jedis.close();
        long end = System.currentTimeMillis();
        System.out.println("not use pipeline batch set total time:" + (end - begin));
    }

    // 批量插入数据到Redis,使用Pipeline
    public static void batchSetUsePipeline() throws Exception {
        Jedis jedis = new Jedis(HOST, PORT);
        Pipeline pipelined = jedis.pipelined();
        String keyPrefix = "pipeline";
        long begin = System.currentTimeMillis();
        for (int i = 1; i < 10000; i++) {
            String key = keyPrefix + "_" + i; 
            String value = String.valueOf(i);
            pipelined.set(key, value);
        }
        pipelined.sync();
        jedis.close();
        long end = System.currentTimeMillis();
        System.out.println("use pipeline batch set total time:" + (end - begin));
    }

    public static void main(String[] args) {    
        try {
            batchSetNotUsePipeline();
            batchSetUsePipeline();      
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

测试结果:

not use pipeline batch get total time:2990
use pipeline batch get total time:41

参考案例:Springboot下redis写入pipelining管道模式性能调优实例

  1. 参考案例:Springboot下redis写入pipelining管道模式性能调优实例
  2. PipelineContext.java

Chain:

链条,一般用在责任链模式中。

责任链模式(Chain of responsibility pattern)可以为某个请求创建一个对象链,每个对象依序检查此请求并对其进行处理或者将它传给链中的下一个对象。

责任链模式,是一种实用性非常强的设计模式,比较典型的应用场景有:

  1. Apache Tomcat 对 Encoding 编码处理的处理
  2. SpringBoot ⾥⾯的拦截器、过滤器链
  3. netty 中的处理链
  4. 支付风控的机制
  5. ⽇志处理级别

通过注解@Order来指定排序,代替手动方法排序sort()

/**
 * 指定注入顺序为1
 *
 */
@Order(1)
@Component
public class RepeatOrderHandleInterceptService implements OrderHandleIntercept {


    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿啄debugIT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值