Restlet 2.0 边学边写(四)Restlet与spring集成

上一次实践是使用Component来发布多个Resource。本次实践将Restlet与spring集成,并使用spring来配置、发布、管理多个Resource。

参考:[url]http://ajaxcn.iteye.com/blog/416913[/url]

本次实践需要导入spring相关包,创建配置文件applicationContext.xml并进行配置。

[b]1.导入spring相关包[/b]
将Restlet安装目录\Edition Java EE\2.0.10\lib下的org.restlet.ext.spring.jar、net.sf.cglib.jar 两个包和spring.jar包加入Build Path,我使用的是spring2.0。

[b]2.Dao[/b]
在com.sunny.restlet.order目录下创建OrderDao接口,代码如下:

package com.sunny.restlet.order;

public interface OrderDao {

public String getOrderById(String orderId);

public String getSubOrderById(String subOrderId);

public String getCustomerById(String custId);

}

接口中定义数据访问方法。

在com.sunny.restlet.order目录下创建OrderDaoImpl类,代码如下:

package com.sunny.restlet.order;

public class OrderDaoImpl implements OrderDao {

public String getCustomerById(String custId) {
// TODO Auto-generated method stub
return "customer" + custId;
}

public String getOrderById(String orderId) {
// TODO Auto-generated method stub
return "order" + orderId;
}

public String getSubOrderById(String subOrderId) {
// TODO Auto-generated method stub
return "subOrder" + subOrderId;
}

}

类实现了OrderDao接口。

[b]3.Resource[/b]
修改com.sunny.restlet.order.CustomerResource类,代码如下:

package com.sunny.restlet.order;

import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

public class CustomerResource extends ServerResource {
String customerId = "";

private OrderDao orderDao;

@Override
protected void doInit() throws ResourceException {
this.customerId = (String) getRequest().getAttributes().get("custId");
}

@Get
public String represent() {
return "get customer id :" + orderDao.getCustomerById(customerId) ;
}

public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}

}

修改后的类相当于spring中的service bean,类中调用了orderDao的数据访问接口。

修改com.sunny.restlet.order.OrderResource类,代码如下:

package com.sunny.restlet.order;

import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

public class OrderResource extends ServerResource {
String orderId = "";
String subOrderId = "";

private OrderDao orderDao;

@Override
protected void doInit() throws ResourceException {
this.orderId = (String) getRequest().getAttributes().get("orderId");
this.subOrderId = (String) getRequest().getAttributes().get(
"subOrderId");
}

@Get
public String represent() {
return "the order id is : " + orderDao.getOrderById(orderId)
+ " and the sub order id is : "
+ orderDao.getSubOrderById(subOrderId);
}

public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
}


[b]4.applicationContext.xml[/b]
在src/下创建spring配置文件applicationContext.xml,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- component -->
<bean id="component" class="org.restlet.ext.spring.SpringComponent">
<property name="defaultTarget" ref="restRouter" />
</bean>
<!-- router -->
<bean id="restRouter" class="org.restlet.ext.spring.SpringBeanRouter">
</bean>
<!-- resource -->
<bean name="/customers/{custId}" id="customerResrouce"
class="com.sunny.restlet.order.CustomerResource" />
<bean name="/orders/{orderId}/{subOrderId}" id="orderResrouce"
class="com.sunny.restlet.order.OrderResource" />
<!-- dao -->
<bean id="orderDao" class="com.sunny.restlet.order.OrderDaoImpl" />
</beans>

文件中定义了component、router、resource和dao,以及将Resource发布到的路径。使用spring不需要再自定义Component和Application。

[b]5.web.xml[/b]
修改web.xml文件,删除原有Restlet配置,加入spring配置,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- SpringServerServlet -->
<servlet>
<servlet-name>restlet</servlet-name>
<servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>component</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>restlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<!-- welcome-file-list -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

文件中定义spring配置文件位置,指定使用SpringServerServlet来提供服务,并定义了在spring配置中Component Bean的id(component)。

[b]6.运行[/b]
重新部署后,通过浏览器访问[url]http://localhost:8080/firstSteps/spring/customers/1[/url]可以看到提示信息
[list]
[*]get customer id :customer1
[/list]
访问[url]http://localhost:8080/firstSteps/spring/orders/1/2[/url]可以看到提示信息
[list]
[*]the order id is : order1 and the sub order id is : subOrder2
[/list]
说明Restlet和spring集成成功。

[b]7.SpringRouter[/b]
SpringBeanRouter要求resource继承ServerResource类,自动将spring BeanFactory中name以"/"开头的bean加到路径上。

除此外还可以使用SpringRouter来集成,applicationContext.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- component -->
<bean id="component" class="org.restlet.ext.spring.SpringComponent">
<property name="defaultTarget" ref="restRouter" />
</bean>
<!-- router -->
<bean id="restRouter" class="org.restlet.ext.spring.SpringRouter">
<property name="attachments">
<map>
<entry key="/customers/{custId}">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="create" bean="customerResource" />
</bean>
</entry>
<entry key="/orders/{orderId}/{subOrderId}">
<bean class="org.restlet.ext.spring.SpringFinder">
<lookup-method name="create" bean="orderResource" />
</bean>
</entry>
</map>
</property>
</bean>
<!-- resource -->
<bean id="customerResource" class="com.sunny.restlet.order.CustomerResource">
</bean>
<bean id="orderResource" class="com.sunny.restlet.order.OrderResource">
</bean>
<!-- dao -->
<bean id="orderDao" class="com.sunny.restlet.order.OrderDaoImpl" />
</beans>

SpringRouter调用SpringFinder,需要指定lookup-method,如果resource继承自Resource,则name="createResource",且resource要实现无参构造函数和init()方法;如果resource继承自ServerResource,则name="create"。详情请参考Restlet源码,SpringFinder类中的createTarget(Request request, Response response)、createResource()和create()方法,SpringBeanFinder类中的create()方法。

[b]8.RestletFrameworkServlet[/b]
还可以使用RestletFrameworkServlet替代SpringServerServlet。这需要导入spring-webmvc.jar包。
修改web.xml,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- SpringServerServlet -->
<servlet>
<servlet-name>restlet</servlet-name>
<servlet-class>org.restlet.ext.spring.RestletFrameworkServlet</servlet-class>
<init-param>
<param-name>targetRestletBeanName</param-name>
<param-value>restRouter</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>restlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<!-- welcome-file-list -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

代码中将targetRestletBeanName这个init-param的值设定为restRouter,如果不设置,默认为root。RestletFrameworkServlet根据这个参数的值从WEB-INF\restlet-servlet.xml中读取配置。
在WEB-INF目录下创建restlet-servlet.xml,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="byName"
xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- router -->
<bean id="restRouter" class="org.restlet.ext.spring.SpringBeanRouter">
</bean>
<!-- resource -->
<bean name="/customers/{custId}" id="customerResrouce"
class="com.sunny.restlet.order.CustomerResource" />
<bean name="/orders/{orderId}/{subOrderId}" id="orderResrouce"
class="com.sunny.restlet.order.OrderResource" />
<!-- dao -->
<bean id="orderDao" class="com.sunny.restlet.order.OrderDaoImpl" />
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值