-
Notifications
You must be signed in to change notification settings - Fork 11
Doc007.WorkWithSpring
在大型webservice开发中,使用spring容器管理各个组件是一个比较好的选择。
happor可以与spring结合,在启动webserver时自动装载spring容器,代码如下:
public static void main(String[] args) {
// TODO Auto-generated method stub
HapporContext context = new HapporSpringContext("web.xml");
context.runServer();
}HapporSpringContext是HapporContext的另一个子类,表示使用spring容器的happor运行环境。构造函数的参数传入spring容器的application context的xml定义文件。
你不用在xml中定义happor所需要的bean(如果你乐意也可以,但是需要关心很多细节)。happor提供了自定义的spring xml tag。
如果我们要创建与最基础的使用方式中的例子一样的运行环境,spring容器的xml配置应该这样:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:happor="http://happor.lechange.cn/schema/tags"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://happor.lechange.cn/schema/tags http://happor.lechange.cn/schema/tags/springtags.xsd">
<!-- happor webserver -->
<happor:server port="8080" timeout="60" maxHttpSize="1000000" executeThreads="16">
<happor:handler class="your.package.name.TestWebserverHandler" />
<happor:controllers>
<happor:controller class="your.package.name.StatisFilter" uriptn="^/thing" />
<happor:controller class="your.package.name.AuthFilter" method="POST" uriptn="^/thing" />
<happor:controller class="your.package.name.GetThingHandler" method="GET" uriptn="^/thing" />
<happor:controller class="your.package.name.PostThingHandler" method="POST" uriptn="^/thing" />
<happor:controller class="your.package.name.DefaultHandler" />
</happor:controllers>
</happor:server>
<!-- other beans here -->
</beans>因为happor自定义了tag,所以首先不要忘记在beans元素中加入相关的namespace属性:xmlns:happor="http://happor.lechange.cn/schema/tags",以及namespace对应的schema:http://happor.lechange.cn/schema/tags http://happor.lechange.cn/schema/tags/springtags.xsd。
happor运行环境通过happor:server元素配置:
- 首先,包含4个属性:
port、timeout、maxHttpSize、executeThreads。 - 其次,包含
happor:handler元素,用于设定webserver回调接口。 - 然后,需要设定controller链,包含
happor:controllers元素。它下面再包含happor:controller元素。每个happor:controller指定一个controller,使用属性class、method、uriptn设置controller的类、匹配method和uriPattern。
happor:controllers元素配置还可以简单一些:
- 使用
happor:controllers的属性package,可以指定controller包名前缀。 - 如果controller的实现类上存在
@Controller注解,那么就不需要给happor:controller设置method和uriptn属性。
简化的happor:controllers如下所示:
<happor:controllers package="your.package.name">
<happor:controller class="StatisFilter" />
<happor:controller class="AuthFilter" />
<happor:controller class="GetThingHandler" />
<happor:controller class="PostThingHandler" />
<happor:controller class="DefaultHandler" />
</happor:controllers>既然代码中可以设置自动扫描controllers,那么在spring容器中也可以配置。将happor:controllers替换为happor:controllers-auto-scan,如下所示:
<happor:controllers-auto-scan>
<happor:filter name="statis" />
<happor:filter name="auth" />
</happor:controllers-auto-scan>在这里,我们要给StatisFilter类加上@Filter("statis")注解,AuthFilter类加上@Filter("auth")注解。
另外,happor:controllers-auto-scan也可以使用属性package来指定自动扫描的包路径。
配置完happor:server元素,使用HapporSpringContext启动程序,spring容器就被装载进来了。