SSM-Mybatis-插件-插件初始化

这篇博客详细介绍了Mybatis中插件的初始化过程。在解析配置文件时,Mybatis会读取插件节点,通过反射创建插件实例,并设置配置参数。这些插件实例随后被保存在Configuration对象的interceptorChain属性中,以List形式存储,便于后续使用。博客内容侧重于理解Mybatis插件的加载机制和配置处理流程。
摘要由CSDN通过智能技术生成

SSM-Mybatis-插件-插件初始化

插件的初始化在Mybatis初始化时完成的,通过XMLConfigBuilder中的代码便可知道:

   
private void pluginElement(XNode parent) throws Exception {
        if (parent != null) {
            Iterator var2 = parent.getChildren().iterator();

            while(var2.hasNext()) {
                XNode child = (XNode)var2.next();
                String interceptor = child.getStringAttribute("interceptor");
                Properties properties = child.getChildrenAsProperties();
                Interceptor interceptorInstance = (Interceptor)this.resolveClass(interceptor).getDeclaredConstructor().newInstance();
                interceptorInstance.setProperties(properties);
                this.configuration.addInterceptor(interceptorInstance);
            }
        }

    }

​ 解析配置文件时,Mybatis的上下文初始化过程中,就开始读入插件节点和配置的参数,同时使用反射技术生成对应的插件实例,然后调用插件方法中的setProperties方法,设置配置参数,将插件实例保存到配置对象中,以便使用它们

插件在Configuration对象中保存:

 public void addInterceptor(Interceptor interceptor) {
        this.interceptorChain.addInterceptor(interceptor);
    }

interceptorChain在Configuration是一个属性,他里面有addInterceptor:


    private final List<Interceptor> interceptors = new ArrayList();
...
      public void addInterceptor(Interceptor interceptor) {
        this.interceptors.add(interceptor);
    }

从代码中看出,完成初始化后的插件保存在List对象里面等待取出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值