URIBroker Service

来源:互联网 发布:python 量化选股策略 编辑:程序博客网 时间:2024/06/03 19:14

1. URIBroker Service

1.1. 概述

Web由有许多资源组成,比如HTML文档,图片,视频, css/javascript等。每一种资源都由一种叫做资源标示符(URI)的符号来定位。URI除了出现在Web页面上,对于程序员来说,有时也会将它作为字面值常量定义在程序当中。

这类静态录入的URI,在早期的系统开发过程中,的确让程序员屡试不爽。但随着系统功能的不断扩充,复杂性越来越高,静态录入URI使得整套系统变得难以扩展和维护。

举个例子,比如你需要对A系统进行移植,但因为早期的URI都是硬编码在程序中,你会慢慢发现所有的URI都需要修改,所有的域名和资源路径都难以统一等等问题。

为了有效解决静态录入URI带来的维护和扩展性问题,URIBroker Service应运而生。

URIBroker Service的特点是以统一的配置和动态渲染的方式来生成URI字符串。在系统重构或移植时,你只需要修改URIBroker Service的统一配置文件 ,无需修改一行代码即可完成全系统URI的无缝迁移。

URLBroker Service从本质上,解决了URI移植性,扩展性差等问题,同时也简化了开发人员的编码工作量。

1.2. 设计

在Web应用中,存在很多种URI,比如静态资源URI,动态资源URI和Servlet风格URI等。URIBroker Service在设计过程中,参考了URI的种类特征。通过进一步抽象,让每个Java接口或抽象类都能对应一种URI,使框架本身的结构更易于理解和使用。下图展示了整个URIBroker Service的静态结构:

1.3. 配置

URIBroker的配置继承了spring-ext风格。下面是一个简单的配置实例:

<uri id="alibabaSite" exposed="true">    <serverURI>http://china.alibaba.com/</serverURI></uri><servlet-uri id="servletLink2">    <contextPath>mycontext1</contextPath>    <servletPath>myservlet1</servletPath></servlet-uri>

uri标签表示定义一个GenericURIBroker。id是这个URIBroker实例的标示。exposed表示这个URIBroker是否被公开。只有当URIBroker被公开后,你才能通过id从URIBroker Service中得到对应的URIBroker实例。

例子中id为adminModule的URIBroker,使用了extends属性来表示它继承自另一个URIBroker。继承后的URIBroker在后续配置当中,可以省略父URIBroker中已定义的属性配置。

contentPath和servletPath表示webapp上下文路径和servlet的访问路径。

1.4. 使用

1.4.1. 在模板中使用uris

在模板中使用uris,需要在pull-tool中配置<uris-tool/>标签。例子如下:

<services:pull xmlns="http://www.alibaba.com/schema/services/pull/factories">    ... // other tools    <uris-tool /></services:pull>

1.4.2. 在代码中使用URIBrokerService

在代码中使用uris,首先需要得到URIBroker Service。URIBroker Service通常会配置在Spring容器中。获得URIBroker Service有两种方式:

第一种是,得到spring容器,然后从容器上下文接口中获取URIBroker Service实例。

第二种是,你将代码所在的bean纳入spring容器当中,利用spring的依赖注入功能自动获得URIBroker Service实例。

下面这个例子演示了通过spring容器上下文获取URIBroker Service实例,并取得URIBroker实例成功渲染URI。

ApplicationContext context = new XmlApplicationContext(new FileSystemResource(new File("services.xml")));URIBrokerService service = (URIBrokerService) context.getBean("uriBrokerService");URIBroker uriBroker = service.getURIBroker("alibabaSite");System.out.println(uriBroker.render());

1.5. URIBrokers

1.5.1. <uri>

<uri>用于渲染通用的uri。它有以下几个部分组成:

URI         = SERVER_INFO + PATH + &quot;?&quot; + QUERY_DATA + &quot;#&quot; + REFERENCESERVER_INFO = scheme://loginUser:loginPassword@serverName:serverPortPATH        = /path/pathQUERY_DATA  = queryKey1=value1&amp;queryKey2=value2REFERENCE   = reference

由<uri>渲染得到的uri:

http://user:pass@myserver.com:8080/path/view.html?id=1#top<uri>的配置实例:<uri id="alibabaSite" exposed="true">    <serverURI>http://china.alibaba.com/</serverURI></uri>

1.5.2. <content-uri>

<content-uri>用于渲染静态资源,比如css, javascript, html等。它有以下几个部分组成:

URI         = SERVER_INFO + PATH + "?" + QUERY_DATA + "#" + REFERENCESERVER_INFO = scheme://loginUser:loginPassword@serverName:serverPortPATH        = /prefixPath/contentPathQUERY_DATA  = queryKey1=value1&queryKey2=value2REFERENCE   = reference

由<content-uri>渲染得到uri:

http://user:pass@myserver.com:8080/prefixPath/contentPath/preview.jpg?id=2#top<content-uri>的配置实例:<content-uri id="clink">    <prefixPath>myprefix1</prefixPath>    <contentPath>mycontent1</contentPath></content-uri>

1.5.3. <servlet-uri>

<serlvet-uri>用于渲染servlet风格的uri。它由以下几个部分组成:

URI         = SERVER_INFO + PATH + "?" + QUERY_DATA + "#" + REFERENCESERVER_INFO = scheme://loginUser:loginPassword@serverName:serverPortPATH        = /contextPath/servletPath/PATH_INFOPATH_INFO   = /pathInfoQUERY_DATA  = queryKey1=value1&queryKey2=value2REFERENCE   = reference

由<servlet-uri>渲染得到的uri:

http://user:pass@myserver.com:8080/mycontext/myservlet/view?id=1#top<servlet-uri>的配置实例:<servlet-uri id="servletLink1">    <contextPath>mycontext1</contextPath>    <servletPath>myservlet1</servletPath></servlet-uri>

1.5.4. <servlet-content-uri>

<servlet-content-uri>用于渲染Servlet风格的静态资源URI。它由以下几个部分组成:

URI         = SERVER_INFO + PATH + "?" + QUERY_DATA + "#" + REFERENCESERVER_INFO = scheme://loginUser:loginPassword@serverName:serverPortPATH        = /contextPath/prefixPath/contentPathQUERY_DATA  = queryKey1=value1&queryKey2=value2REFERENCE   = reference

由<servlet-content-uri>渲染得到的uri:

 http://user:pass@myserver.com:8080/mycontext/view.html?id=1#top<servelt-content-uri>的配置实例:<uris:servlet-content-uri id="scontentLink2">    <uris:contextPath>mycontext2</uris:contextPath>    <uris:prefixPath>myprefix2</uris:prefixPath>    <uris:contentPath>mycontent2</uris:contentPath></uris:servlet-content-uri>

1.5.5. <uri-bean>

<uri-bean>是一个特殊的uri标签。它允许用户根据自己的需求编写定制化的UIRBorker,并配置到URIBroker Service中。

编写定制的URIBroker,可参照本章第二节“设计”中给出的静态结构图。继承当中任一URIBroker,进行扩展。

<uri-bean>的配置实例:

 <uris:uri-bean id="mybean" class="com.alibaba.citrus.service.uribroker.uri.GenericURIBroker" exposed="true" extends="mybeanBase" />

1.6. Interceptors

在URIBroker中,也存在拦截器。拦截器的作用是当框架按照指定配置渲染uri时,对渲染过程进行拦截,在渲染的前或后植入一些变更上下文的操作。

目前框架已经提供的interceptors有:

uri-interceptors:with-redirect-location-token

这个拦截器会在生成外部重定向URL时,在QUERY_DATA中加入token。

interceptors:randomize

1.7. 技巧和提示

1.7.1. 在模板中重用uri

在模板中,我们经常会这样定义变量:

#set ($newuri = $myuri.setPath("xxx"))#foreach ($i in [1..10])  <a href="$newuri.addQueryData("id", $i)">...</a>#end

其实这种写法是错误的。如果myuri的值是null,可能会导致下面a标签的uri渲染异常。正确的写法是:

 #set ($newuri = $myuri.setPath("xxx").fork())

1.7.2. 用fork提升性能

#set ($newuri = $myuri.setPath("xxx")) // first#set ($newuri = $myuri.setPath("xxx").fork()) // secondfork uri的性能要优于不fork。即第二种写法的性能要好于第一种。

1.8. 总结

本章主要介绍了URIBroker Service的框架结构,用途,以及配置使用案例。

0 0
原创粉丝点击