58同城开源的轻量级web框架 https://github.com/58code/Argo
随着公司规模的不断扩大,项目越来越多了,单个项目投入的人也多了起来,每个程序员都有自己的一套编码风格。身为一个处女座程序员,深深感觉到无法忍受一团乱麻似的工程。于是就需要一套强有力的规范,而且规范最好能够分级,最低层的规范最为严格,导致大家写出的代码都能差不多,也就减少人员交叉过渡的成本,越靠近业务顶层的规范就越松散、又根据业务相互隔离、可插拔,这样一来,即使无法维护,重写的成本也会降低。
一个自己定义的web框架所要完成的任务恰好包括了从低到高的全部过程,如果您的公司已经完成了服务化架构,那么web也就剩下从中高层到顶层的过程。Argo就是只关注解耦后的业务层web框架,服务化框架(分布式通讯中间件)是另一个开源项目,叫Gaea。
58开源的官方微博http://weibo.com/58code
controller一个必不可少的功能:拦截器。
上一篇中我有一个地方一带而过,就是Router 的默认实现DefaultRouter
this.actions = buildActions(argo, controllerClasses, staticAction);
DefaultRouter的构造方法有注解@Inject,实例化是通过Guice
@Inject public DefaultRouter(Argo argo, @ArgoSystem Set<Class<? extends ArgoController>> controllerClasses, @StaticActionAnnotation Action staticAction) { this.argo = argo; argo.getLogger().info("initializing a %s(implements Router)", this.getClass()); this.actions = buildActions(argo, controllerClasses, staticAction); argo.getLogger().info("%s(implements Router) constructed.", this.getClass()); }
那么这些参数是从哪来的,就是之前提到的com.bj58.argo.inject.ArgoModule,Argo的绑定关系都能在这里找到。
第一个参数Argo的提供实例为
@Provides @Singleton private Argo provideArgo() { return argo; }
第二个参数带注解的 @ArgoSystem Set<Class<? extends ArgoController>> controllerClasses 提供实例为
@Provides @ArgoSystem @Singleton private Set<Class<? extends ArgoController>> provideControllerClasses() { return argo.getControllerClasses(); }第三个参数带注解的 @StaticActionAnnotation Action staticAction ,绑定在configure()方法中。顺便提一下,这个StaticFilesAction是处理静态文件的,跟tomcat中的DefaultServlet一样,只是有个指定的读取路径
bind(Action.class).annotatedWith(StaticActionAnnotation.class) .to(StaticFilesAction.class);
好了,DefaultRouter的实参来源也知道了,构造方法中buildActions方法的调用,还有一层buildActions方法调用,我们来看看这个
List<Action> buildActions(Set<ArgoController> controllers, Action staticAction) { List<Action> actions = Lists.newArrayList(); actions.add(staticAction); for (ArgoController controller : controllers) { ControllerInfo controllerInfo = new ControllerInfo(controller); List<ActionInfo> subActions = controllerInfo.analyze(); for(ActionInfo newAction : subActions) merge(actions, MethodAction.create(newAction)); } return ImmutableList.copyOf(actions); }这个段代码看出,全局变量 private final List<Action> actions; 就是具体的Action集合
代码详细描述了首先把静态资源action加入全局actions中,再把从controller类中解析出的ActionInfo集合合并到actions中。其中 merge(actions, MethodAction.create(newAction)); 这段的MethodAction,就是方法action。
@Override public ActionResult route(BeatContext beat) { RouteBag bag = RouteBag.create(beat); for(Action action : actions) { RouteResult routeResult = action.matchAndInvoke(bag); if (routeResult.isSuccess()) return routeResult.getResult(); } return ActionResult.NULL; }上面这段代码是处理请求过程,执行action的matchAndInvoke方法,静态文件action就是读取指定目录的文件返回,方法action需要看看
@Override public RouteResult matchAndInvoke(RouteBag bag) { if (!actionInfo.matchHttpMethod(bag)) return RouteResult.unMatch(); Map<String, String> uriTemplateVariables = Maps.newHashMap(); boolean match = actionInfo.match(bag, uriTemplateVariables); if (!match) return RouteResult.unMatch(); // PreIntercept for(PreInterceptor preInterceptor : actionInfo.getPreInterceptors()) { ActionResult actionResult = preInterceptor.preExecute(bag.getBeat()); if (ActionResult.NULL != actionResult) return RouteResult.invoked(actionResult); } ActionResult actionResult = actionInfo.invoke(uriTemplateVariables); // PostIntercept for(PostInterceptor postInterceptor : actionInfo.getPostInterceptors()) { actionResult = postInterceptor.postExecute(bag.getBeat(), actionResult); } return RouteResult.invoked(actionResult); }PreInterceptor是前置拦截器,PostInterceptor是后置拦截器,ActionInfo封装时就根据注解把拦截器都加进去了,执行请求的时候再拿出来按顺序走一遍。拦截器的用法也很简单。
相关推荐
58同城的web框架 Argo ,Argo是开源的web框架,目前Argo支撑着58同城几乎所有的web站点,包括wap和手机端的访问...
Argo是开源的web框架,目前Argo支撑着58同城几乎所有的web站点,包括wap和手机端的访问等,现在wf每天处理10亿级的请求。经过长时间的运作与运行,证明Argo是一个可靠的、高效的web框架。 Argo在wf做了大量优化和...
ArgoUML-0.26开源.exe 推荐下载
代码超级干净,可以很容易地定制,很容易转化为任何类型的web应用程序,包括自定义管理面板,数据分析仪表盘,电子商务后端、CMS、CRM或任何SASS面板。 主要特色 响应布局(台式电脑、平板电脑、移动设备) 用Bootstrap...
zooviewer zookeeper web ui written in java.jsut like node-zk-web 这是一个查看zookeeper节点并可以...1,基于58开源的web框架Argo,全应用无一个配置文件。详见. 2,页业端用了jquery-tree ,方便查看zk的树状节点.
argouml一款很好的开源uml工具 中文版的
Argo是一个开源原生容器工作流引擎用于在Kubernetes上开发和运行应用程序
ArgoUML使用说明,官方英文版 ArgoUML是一个领先的开源UML模型工具,它支持UML 1.4的所有标准,可以运行于任何Java平台上。
ARGO入门手册1
argo源码类图,运行时序图
统一建模语言 (UML) 用于设计软件。 ArgoUML 是一个开源的 UML 编辑器。 该插件将“组件的基于上下文的约束语言(CCL)”集成到 ArgoUML 中。 在 Berlios,我们说德语 - 查看 www.cocons.or
Argo数据的说明手册
BGC_ARGO数据,叶绿素浓度
argo-UML 是开源的uml工具。 通过学习源代码来加强自己的实力。
UML argo建模工具下载,很实用的一个建模工具,方便实用,供喜欢的朋友交流使用
ArgoUML开源的uml设计软件,可以和php集成!!
ArgoUML是一个领先的开源UML模型工具,它支持UML 1.4的所有标准,可以运行于任何Java平台上。---来自《开源中国》https://www.oschina.net/p/argouml?hmsr=aladdin1e1
从官网下的ArgoUML-0.34 用来画UML图
标签:argo-2.4.jar.zip,argo,2.4,jar.zip包下载,依赖包
标签:argo-2.8.jar.zip,argo,2.8,jar.zip包下载,依赖包