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对象里面等待取出