Activiti核心的骨架是:命令模式+职责链 本文的代码版本:activiti 5.22
一、职责链 1、什么是职责链模式http://blog.csdn.net/jiankunking/article/details/50443294
2、Activiti中职责链初始化流程初始化流程引擎的时候会初始化很多信息,具体如下:
protected void init() {
initConfigurators();
configuratorsBeforeInit();
initProcessDiagramGenerator();
initHistoryLevel();
initExpressionManager();
initDataSource();
initVariableTypes();
initBeans();
initFormEngines();
initFormTypes();
initScriptingEngines();
initClock();
initBusinessCalendarManager();
initCommandContextFactory();
initTransactionContextFactory();
initCommandExecutors();
initServices();
initIdGenerator();
initDeployers();
initJobHandlers();
initJobExecutor();
initAsyncExecutor();
initTransactionFactory();
initSqlSessionFactory();
initSessionFactories();
initJpa();
initDelegateInterceptor();
initEventHandlers();
initFailedJobCommandFactory();
initEventDispatcher();
initProcessValidator();
initDatabaseEventLogging();
configuratorsAfterInit();
}
由于本文主要探究:职责链与命令模式,所以这里只需要关注initCommandExecutors()即可,在该方法中会进行如下初始化:
protected void initCommandExecutors() {
//初始化命令上下文
initDefaultCommandConfig();
initSchemaCommandConfig();
//初始化CommandInvoker
initCommandInvoker();
//初始化命令拦截器
//默认会添加LogInterceptor、TransactionInterceptor、CommandContextInterceptor、CommandInterceptor拦截器
initCommandInterceptors();
//初始化命令执行器
//先初始化拦截器之间的指向关系,并获取到职责链的第一个元素first
//根据CommandConfig与first来初始化CommandExecutorImpl类的实例
initCommandExecutor();
}
后面三个方法具体代码如下:
protected List customPreCommandInterceptors;
protected List customPostCommandInterceptors;
protected void initCommandInvoker() {
if (commandInvoker==null) {
commandInvoker = new CommandInvoker();
}
}
protected void initCommandInterceptors() {
if (commandInterceptors==null) {
commandInterceptors = new ArrayList();
if (customPreCommandInterceptors!=null) {
commandInterceptors.addAll(customPreCommandInterceptors);
}
commandInterceptors.addAll(getDefaultCommandInterceptors());
if (customPostCommandInterceptors!=null) {
commandInterceptors.addAll(customPostCommandInterceptors);
}
//命令调用器,拦截器最后的一个,为调用具体的命令
commandInterceptors.add(commandInvoker);
}
}
protected void initCommandExecutor() {
if (commandExecutor==null) {
CommandInterceptor first = initInterceptorChain(commandInterceptors);
commandExecutor = new CommandExecutorImpl(getDefaultCommandConfig(), first);
}
}
方法之间的调用关系及初始化了哪些信息如下图所示:
最终命令拦截器链就是: customPreCommandInterceptors->LogInterceptor->TransactionInterceptor->CommandContextInterceptor->customPostCommandInterceptors->commandInvoker
注意:commandInvoker是CommandInterceptor的实例,即最后拦截的是具体命令的执行。
在初始化过程中主要用到几个接口结构如下:
通过上面两部分大家应该清楚了,activiti职责链:是在哪?如何初始化?这个两个问题了。 那么我们应该如何自定义一个拦截器,并指定其在职责链中的位置呢? 其实,很简单:
1、自定义拦截器public class InterceptorTest extends AbstractCommandInterceptor {
@Override
public T execute(CommandConfig config, Command command) {
//输出字符串和命令
System.out.println("this is InterceptorTest----->" + command.getClass().getName());
//然后让职责链中的下一个请求处理者处理命令
return next.execute(config, command);
}
}
2、继承ProcessEngineConfigurationImpl根据需要重写其中的方法
public class MyProcessEngineConfiguration extends ProcessEngineConfigurationImpl {
@Override
protected CommandInterceptor createTransactionInterceptor() {
// 事务拦截器 在activiti默认初始化的时候是空的
// 此处返回自定义拦截器InterceptorTest2
return new InterceptorTest2();
}
@Override
protected Collection
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?