ActivitiUEL表达式是如何与Spring的容器整合起来的
Activiti UEL 表达式
UEL表达式到底是什么呢?
UEL是java EE6规范的⼀部分,UEL(Unified Expression Language)即统⼀表达式语⾔,activiti⽀持两个UEL表达式:UEL-value和UEL-method
Activiti使⽤UEL进⾏表达式解析(有关详细信息,请参阅)
Activiti 如何使⽤UEL表达式获取Spring 容器中的bean?
答案在activiti-spring.jar中
SpringExpressionManager.java //spring表达式管理类
public class SpringExpressionManager extends ExpressionManager {
//应⽤上下⽂
protected ApplicationContext applicationContext;
public SpringExpressionManager(ApplicationContext applicationContext, Map<Object, Object> beans) {
super(beans);//这个beans是流程引擎中的beans
this.applicationContext = applicationContext;
}
@Override
protected ELResolver createElResolver(VariableScope variableScope) {
//复合el表达式
CompositeELResolver compositeElResolver = new CompositeELResolver();
compositeElResolver.add(new VariableScopeElResolver(variableScope));
//判断beans是否为空
if (beans != null) {
// 在表达式中只暴露流程引擎中的beans
compositeElResolver.add(new ReadOnlyMapELResolver(beans));
} else {
// 在表达式中暴露整个application-context
compositeElResolver.add(new ApplicationContextElResolver(applicationContext));
}
//添加数组解析器
compositeElResolver.add(new ArrayELResolver());
//添加集合解析器
compositeElResolver.add(new ListELResolver());
//添加map解析器
compositeElResolver.add(new MapELResolver());
//添加json格式的数据解析器 like {key:value,...} or [1,2,3,4,...]
compositeElResolver.add(new JsonNodeELResolver());
//添加bean解析器
compositeElResolver.add(new BeanELResolver());
return compositeElResolver;
}
在ProcessEngineConfigurationImpl.java中调⽤
public class ProcessEngineFactoryBean implements FactoryBean<ProcessEngine>, DisposableBean, ApplicationContextAware {
...
protected void configureExpressionManager() {
if (ExpressionManager() == null && applicationContext != null) {
processEngineConfiguration.setExpressionManager(new SpringExpressionManager(applicationContext, Beans()));
}
}
...
}
使⽤表达式
Activiti的Expression 接⼝
public interface Expression extends Serializable {
// variableScope 为变量作⽤域
Object getValue(VariableScope variableScope);
void setValue(Object value, VariableScope variableScope);
String getExpressionText();
}
在代码中的具体使⽤
//获取表达式对象
//execution 为 VariableScope 的实现类⽤于从不同的变量作⽤域中取变量el表达式获取map的值
Expression expression = ateExpression("${bean.id}");
businessKey = Value(execution).toString();
为什么有VariableScope
因为Activiti中有很多流程,每⼀个流程都有很多变量,使⽤变量的时候肯定不能去拿其他流程的变量,为了更好的管理在表达式中使⽤变
量,所以这⾥就有了⼀个变量作⽤域的概念
通过变量作⽤域获得EL上下⽂
public class ExpressionManager {
...
public ELContext getElContext(VariableScope variableScope) {
ELContext elContext = null;
if (variableScope instanceof VariableScopeImpl) {
VariableScopeImpl variableScopeImpl = (VariableScopeImpl) variableScope;
elContext = CachedElContext();
}
if (elContext == null) {
//如果没有就创建⼀个EL上下⽂
elContext = createElContext(variableScope);
if (variableScope instanceof VariableScopeImpl) {
((VariableScopeImpl) variableScope).setCachedElContext(elContext);
}
}
return elContext;
}
...
}
VariableScope 的实现类
实现类就只要有三个类NoExecutionVariableScope.java
TaskEntityImpl.java
ExecutionEntityImpl.java
其中VariableScopeImpl 是个抽象类,这⾥使⽤了模板模式
需要⼦类实现的⽅法JUEL 实现类
protected abstract Collection<VariableInstanceEntity> loadVariableInstances ();
protected abstract VariableScopeImpl getParentVariableScope ();
protected abstract void initializeVariableInstanceBackPointer (VariableInstanceEntity variableInstance);
protected abstract VariableInstanceEntity getSpecificVariable (String variableName);
public class JuelExpression implements Expression {
protected String expressionText;
protected ValueExpression valueExpression;
public JuelExpression(ValueExpression valueExpression, String expressionText) {
this.valueExpression = valueExpression;
}
public Object getValue(VariableScope variableScope) {
//获取值的时候都要通过variableScope获取上下⽂
ELContext elContext = ProcessEngineConfiguration().getExpressionManager().getElContext(variableScope); try {
ExpressionGetInvocation invocation = new ExpressionGetInvocation(valueExpression, elContext);
InvocationResult();
} catch (PropertyNotFoundException pnfe) {
throw new ActivitiException("Unknown property used in expression: " + expressionText, pnfe);
} catch (MethodNotFoundException mnfe) {
throw new ActivitiException("Unknown method used in expression: " + expressionText, mnfe);
} catch (ELException ele) {
throw new ActivitiException("Error while evaluating expression: " + expressionText, ele);
} catch (Exception e) {
throw new ActivitiException("Error while evaluating expression: " + expressionText, e);
}
}
public void setValue(Object value, VariableScope variableScope) {
ELContext elContext = ProcessEngineConfiguration().getExpressionManager().getElContext(variableScope); try {
ExpressionSetInvocation invocation = new ExpressionSetInvocation(valueExpression, elContext, value);
} catch (Exception e) {
throw new ActivitiException("Error while evaluating expression: " + expressionText, e);
}
}
@Override
public String toString() {
if (valueExpression != null) {
ExpressionString();
}
String();
}
public String getExpressionText() {
return expressionText;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论