Webx pull service
1.概述
pull service的功能是将对象置入模板中。被pull service放到模板中的对象,不需要应用程序的干预即可直接使用。如果模板没有用到某个对象,则不会产生创建该对象的开销。看起来,这些对象像是被模板主动“拉”进context的,而不是由应用程序push进context中的。这就是pull service名称的来源。
2.作用域
global :就是singleton,在系统启动时创建实例。
request :在每个request的第一次访问该tool时,自动创建实例。
注意,!ToolFactory和ToolSetFactory本身一定是singleton的。但他们所创建的tool对象的作用域,是由ToolFactory或ToolSetFactory.isSingleton方法决定的。
3.全局性
parent context中的tools被所有子context中的tools共享。但子context可以覆盖父context中的同名tools(注:只是以当前context的tool的引用为优先,不影响父context中的实例)。
4.使用
<services:pull xmlns="http://www.alibaba.com/schema/services/pull/factories">
< … />
/services:pull>
5.ID命令约定
<tool id="xxx">,(明确指定)此时id=xxx
<tool class="com....!HelloTool>,此时id=hello
<tool class="com....Hello>,此时id=hello
<hello-tool > 此时id=hello
6.共享实例
如果每个sub context中分别配置pull service,那么即使同名的tool,在内存中也是两份实例。
如果希望多个sub context中共享一个实例,就把它定义在parent context中。
7.使用方法
package com.alibaba.webx.tutorial1.app1.tool; import java.text.SimpleDateFormat;
import java.util.Date; public class MyPullTool{ public static String method1(){
return "hello, xiao xiong!";
} public String method2(Date date){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String new_date = format.format(date);
return new_date;
}
}
在webx.xml配置
<services:pull xmlns="http://www.alibaba.com/schema/services/pull/factories">
<utils />
<page-tool />
<control-tool />
<uris-tool />
<bean-tool id="mytool" class="com.alibaba.webx.tutorial1.app1.tool.MyPullTool" scope="global" autowire="true"/>
</services:pull>
在vm中使用方式
$rundata.response.setContentType("text/html")
$rundata.setLayoutEnabled(false)
<p>Welcome!</p>
今天是 $mytool.method2($date)
优势:使用bean-tool
1你可以方便的将已有的一个工具类配置一下就可以变成PullTool而不需要专门实现任何接口、去除了对原有类的不必要侵入,
2可以将Spring容器的一个Bean作为PullTool使用
8.扩展
8.1.1 创建新的tool factory
public class MyToolFactory implements ToolFactory {
private boolean singleton = true;
private DomTool tool;
public void setSingleton(boolean singleton) {
this.singleton = singleton;
}
public Object createTool() throws Exception {
if (this.singleton) {
if (tool == null) {
tool = new DomTool();
}
return tool;
} else {
return new DomTool();
}
}
public boolean isSingleton() {
return this.singleton;
}
}
public class DomTool {
public void addScript(String scriptURI) {
}
public void addCss(String cssURI) {
}
}
8.1.2 配置
<factory id="domTool" class="com.alibaba.sample.showcase.commons.pull.MyToolFactory" p:singleton="true" />
8.1.3 模板中使用
$domTool.addScript("scripts/test.js");
8.2.1 实现ToolSetFactory
public class MyToolSetFactory implements ToolSetFactory {
private boolean singleton = true;
Map<String, Object> tools = new ConcurrentHashMap<String, Object>();
public MyToolSetFactory() {
tools.put("toolA", new ToolA());
tools.put("toolB", new ToolB());
}
public void setSingleton(boolean singleton) {
this.singleton = singleton;
}
public Object createTool(String name) throws Exception {
Object tool = null;
if (this.singleton) {
tool = tools.get(name);
} else {
tool = this.makeTool(name);
}
if (tool == null) {
throw new RuntimeException("tool '" + name + "' is not found");
}
return tool;
}
public Iterable<String> getToolNames() {
return this.tools.keySet();
}
public boolean isSingleton() {
return this.singleton;
}
private Object makeTool(String name) {
if ("toolA".equals(name)) {
return new ToolA();
} else if ("toolB".equals(name)) {
return new ToolB();
}
return null;
}
}
8.2.2 配置
<factory id="myToolSet" class="com.alibaba.sample.showcase.commons.pull.MyToolSetFactory" p:singleton="true" />
8.2.3 使用
$toolA.say("hello toolA")
$toolB.sing("happy birthday")
Webx pull service的更多相关文章
- Webx框架自带的petstore
Webx框架:http://openwebx.org/ petstore:webx3/webx-sample/petstore/tags/3.0/petstore 编译之后:mvn jetty:run ...
- Webx MVC分析(转)
Webx框架:http://openwebx.org/ petstore:webx3/webx-sample/petstore/tags/3.0/petstore 编译之后:mvn jetty:run ...
- Webx示例-PetStore分析1
1. 下载源码 2. 启动容器,加载组件--WebxContextLoaderListener WebxContextLoaderListener继承自org.springframework.web. ...
- pull类型消息中间件-消息发布者(一)
消息集群架构 对于发送方来说的关键几要素 topic 消息的主题,由用户定义.类似于知乎的话题,Producer发送消息的时候需要指定发送到某一个topic下面,Consumer从某一个topic下面 ...
- dubbo-admin-2.5.3 运行报错: Bean property 'URIType' is not writable or has an invalid 解决方法
因为 jdk 是1.8的版本,和 dubbo-admin 存在兼容性问题.所以报错: Bean property 'URIType' is not writable or has an invalid ...
- webx--petstore
配置对应环境,运行petstore 通过官网给的命令行方法,来运行petstore petstore是java ee的经典学习案例,下载链接 如何运行呢? 参见官网给的指导:webx官网 git cl ...
- dubbo入门学习
官方网址:http://dubbo.apache.org/zh-cn/index.html 学习可以参考官网中文文档:http://dubbo.apache.org/zh-cn/docs/user/q ...
- RocketMQ源码 — 三、 Producer消息发送过程
Producer 消息发送 producer start producer启动过程如下图 public void start(final boolean startFactory) throws MQ ...
- 使用docker-compose 大杀器来部署服务 上
使用docker-compose 大杀器来部署服务 上 我们都听过或者用过 docker,然而使用方式却是仅仅用手动的方式,这样去操作 docker 还是很原始. 好吧,可能在小白的眼中噼里啪啦的对着 ...
随机推荐
- LeetCode总结 -- 高精度篇
我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...
- js 数组,字符串,JSON,bind, Name
/** * Created by W.J.Chang on 2014/5/23. */ // 判读是否是数组的方法 console.log(Array.isArray(new Array)); con ...
- Unity 3d 实施刚体力
1.选中已经添加了刚体的物体,然后添加恒定力组件. 此组件可以给刚体中添加恒定的力或扭矩力,常用于一次性发射的刚体,如模拟火箭的发射.这种物体的初始速度不是很大,但是随着时间的推移,加速度会越来越大. ...
- win8.1 安装webaccess遇到的写访问权限错误
大学的时候闲暇时间都用来玩游戏了,现在工作了,就把工作中遇到的问题和学习心得跟大家共享下,若有不对的地方欢迎指出,谢谢! 前几天突然想换用vs2013开发,就重装了win8.1系统,在新系统上安装we ...
- win7环境下安装MongoDB
1.从http://www.mongodb.org/downloads获取,下载适合windows版本的mongodb,注意32位和64位的区别2.将下载的zip版本,解压到D:/mongodb3.创 ...
- iOS开发篇-AFNetworking 上传和下载
最近用到了关于AFNetworking的上传和下载问题,顺便写到博客中,以供大家参考和研究. //下载NSURLSessionConfiguration *configuration = [NSURL ...
- google base之LockImpl
为了兼容不同的平台,这个类采用了impl模式,win平台通过CRITICAL_SECTION, 这样的话还是相对比较简单,具体就不详解了,不过不得不说boost的实现方式就要复杂到哪里去了,当然,好处 ...
- [C#技术参考]在PictureBox 中绘图防止闪烁的办法
开篇之前说点别的,马上年终了,好希望年终奖大大的,但是好像这次项目的展示很重要,所以这几天绷得比较近,但是真的没有感觉烦,就是害怕来不及.所以抓紧了.下面直接正题.说一下用到的东西,都是Google搜 ...
- asp.net core + angular2 的环境配置
国内整个对 asp.net core 和 angular2这些新出来的关注度不是太好.跟国外比很大差距. 我在试着去做这个整合的时候也碰到不少问题. 最后通过查阅大量资料才弄明白. 我想肯定也会有类 ...
- Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发
原文:Intellij IDEA + Android SDK + Genymotion Emulator打造最佳Android开发 Intellij IDEA + Android SDK + Geny ...