Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
皇甫嗷嗷叫
qlexpress
提交
4e9ccbaf
Q
qlexpress
项目概览
皇甫嗷嗷叫
/
qlexpress
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Q
qlexpress
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4e9ccbaf
编写于
1月 09, 2018
作者:
T
tianqiao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
537d710a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
89 addition
and
0 deletion
+89
-0
README.md
README.md
+89
-0
未找到文件。
README.md
浏览文件 @
4e9ccbaf
...
...
@@ -457,4 +457,93 @@ InstructionSet instructionSet = expressRunner.parseInstructionSet(expressString)
InstructionSet getInstructionSetFromLocalCache(String expressString);
//清除缓存
void clearExpressCache();
```
### (6)增强上下文参数Context相关的api
#### 6.1 与spring框架的无缝集成
上下文参数 IExpressContext context 非常有用,它允许put任何变量,然后在脚本中识别出来。
在实际中我们很希望能够无缝的集成到spring框架中,可以仿照下面的例子使用一个子类。
```
java
public class QLExpressContext extends HashMap
<String
,
Object
>
implements
IExpressContext
<String
,
Object
>
{
private ApplicationContext context;
//构造函数,传入context和 ApplicationContext
public QLExpressContext(Map<String, Object> map,
ApplicationContext aContext) {
super(map);
this.context = aContext;
}
/**
* 抽象方法:根据名称从属性列表中提取属性值
*/
public Object get(Object name) {
Object result = null;
result = super.get(name);
try {
if (result == null && this.context != null
&& this.context.containsBean((String) name)) {
// 如果在Spring容器中包含bean,则返回String的Bean
result = this.context.getBean((String) name);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
public Object put(String name, Object object) {
return super.put(name, object);
}
}
```
完整的demo参照 [SpringDemoTest.java](https://github.com/alibaba/QLExpress/blob/master/src/test/java/com/ql/util/express/test/spring/SpringDemoTest.java)
#### 6.2 自定义函数操作符获取原始的context控制上下文
自定义的Operator需要直接继承OperatorBase,获取到parent即可,可以用于在运行一组脚本的时候,直接编辑上下文信息,业务逻辑处理上也非常有用。
```
java
public class ContextMessagePutTest {
class OperatorContextPut extends OperatorBase {
public OperatorContextPut(String aName) {
this.name = aName;
}
@Override
public OperateData executeInner(InstructionSetContext parent, ArraySwap list) throws Exception {
String key = list.get(0).toString();
Object value = list.get(1);
parent.put(key,value);
return null;
}
}
@Test
public void test() throws Exception{
ExpressRunner runner = new ExpressRunner();
OperatorBase op = new OperatorContextPut("contextPut");
runner.addFunction("contextPut",op);
String exp = "contextPut('success','false');contextPut('error','错误信息');contextPut('warning','提醒信息')";
IExpressContext
<String
,
Object
>
context = new DefaultContext
<String
,
Object
>
();
context.put("success","true");
Object result = runner.execute(exp,context,null,false,true);
System.out.println(result);
System.out.println(context);
}
}
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录