点进去,Activiti框架具体构建buildProcessEngine方法如下,其中 this.init()的作用是环境初始化,包括配置设置、JDBC连接、bean装载等的:
public ProcessEngine buildProcessEngine() { this.init(); ProcessEngineImpl processEngine = new ProcessEngineImpl(this); if (this.isActiviti5CompatibilityEnabled && this.activiti5CompatibilityHandler != null) { Context.setProcessEngineConfiguration(processEngine.getProcessEngineConfiguration()); this.activiti5CompatibilityHandler.getRawProcessEngine(); } this.postProcessEngineInitialisation(); return processEngine; }在this.init()方法里,涉及到责任链模式初始化的方法是this.initCommandExecutors(),里面详情如下:
public void initCommandExecutors() { this.initDefaultCommandConfig(); this.initSchemaCommandConfig(); //初始化命令调用器 this.initCommandInvoker(); //List存放进涉及到的拦截器 this.initCommandInterceptors(); //初始化命令执行器 this.initCommandExecutor(); }这里只需要关注最后三个方法——
this.initCommandInvoker()
initCommandInvoker()初始化构建了一个CommandInvoker拦截器,它继承上边提到的拦截器抽象类AbstractCommandInterceptor。这个拦截器在整条过滤器链中是最重要和关键,它排在了整条链的最后,其实,它才是最终执行请求的,前边几个拦截器都是传递请求而已。
public void initCommandInvoker() { if (this.commandInvoker == null) { if (this.enableVerboseExecutionTreeLogging) { this.commandInvoker = new DebugCommandInvoker(); } else { //初始化执行该行代码 this.commandInvoker = new CommandInvoker(); } } }这里 new CommandInvoker()一个对象,然后将地址复制给this.commandInvoker对象引用,注意,该引用将会用在接下来的initCommandInterceptors()方法里——
this.initCommandInterceptors();
initCommandInterceptors方法主要作用是创建一个List集合,然后将需要用到的拦截器都保存到该List集合里——
public void initCommandInterceptors() { if (this.commandInterceptors == null) { this.commandInterceptors = new ArrayList(); if (this.customPreCommandInterceptors != null) { //用户自定义前置拦截器 this.commandInterceptors.addAll(this.customPreCommandInterceptors); } //框架自带默认的拦截器 this.commandInterceptors.addAll(this.getDefaultCommandInterceptors()); if (this.customPostCommandInterceptors != null) { this.commandInterceptors.addAll(this.customPostCommandInterceptors); } //命令调用器,在拦截器链最后一个 this.commandInterceptors.add(this.commandInvoker); } }this.getDefaultCommandInterceptors()的代码如下:
public Collection<? extends CommandInterceptor> getDefaultCommandInterceptors() { List<CommandInterceptor> interceptors = new ArrayList(); //日志拦截器 interceptors.add(new LogInterceptor()); CommandInterceptor transactionInterceptor = this.createTransactionInterceptor(); if (transactionInterceptor != null) { interceptors.add(transactionInterceptor); } // if (this.commandContextFactory != null) { interceptors.add(new CommandContextInterceptor(this.commandContextFactory, this)); } //事务拦截器 if (this.transactionContextFactory != null) { interceptors.add(new TransactionContextInterceptor(this.transactionContextFactory)); } return interceptors; }可见,方法里的 this.commandInterceptors 就是一个专门储存拦截器对象的List集合——
protected List<CommandInterceptor> commandInterceptors;这里只需要重点关注this.commandInterceptors.add(this.commandInvoker)这行代码,就是将上边创建的CommandInvoker拦截器对象存储到List里,它是放在initCommandInterceptors()方法最后,某种程度也就意味着,这个拦截器在整条链当中处在最后面的位置。
执行完该this.initCommandInterceptors()方法后,就可获取到所有的拦截器对象,到这一步时,各拦截器还是互相独立的,仍无法通过next()来进行调用传递,那么,究竟是如何将它们串起来形成一条链呢?
接下来的this.initCommandExecutor()方法,就是实现将各拦截器串起来形成一条长链。
this.initCommandExecutor();
该方法有两个作用,一个是生成Interceptor拦截器链,一个是创建命令执行器commandExecutor。
public void initCommandExecutor() { if (this.commandExecutor == null) { CommandInterceptor first = this.initInterceptorChain(this.commandInterceptors); this.commandExecutor = new CommandExecutorImpl(this.getDefaultCommandConfig(), first); } }this.initInterceptorChain(this.commandInterceptors)是将集合里的拦截器初始化生成一条拦截器链,先循环获取List集合里的拦截器对象chain.get(i),然后通过setNext()方法在该拦截器对象chain.get(i)里设置下一个拦截器引用,这样,就可实现责任链里所谓每个接收者都包含对另一个接收者的引用的功能。
public CommandInterceptor initInterceptorChain(List<CommandInterceptor> chain) { if (chain != null && !chain.isEmpty()) { for(int i = 0; i < chain.size() - 1; ++i) { ((CommandInterceptor)chain.get(i)).setNext((CommandInterceptor)chain.get(i + 1)); } return (CommandInterceptor)chain.get(0); } else { throw new ActivitiException("invalid command interceptor chain configuration: " + chain); } }那么,这条拦截器链当中,都有哪些拦截器呢?