Restlet 2.0 边学边写(四)Restlet与spring集成
上一次实践是使用Component来发布多个Resource。本次实践将Restlet与spring集成,并使用spring来配置、发布、管理多个Resource。
参考:http://ajaxcn.iteye.com/blog/416913
本次实践需要导入spring相关包,创建配置文件applicationContext.xml并进行配置。
1.导入spring相关包
将Restlet安装目录\Edition Java EE\2.0.10\lib下的org.restlet.ext.spring.jar、net.sf.cglib.jar 两个包和spring.jar包加入Build Path,我使用的是spring2.0。
2.Dao
在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);}
package com.sunny.restlet.order;public class OrderDaoImpl implements OrderDao {public String getCustomerById(String custId) {// TODO Auto-generated method stubreturn "customer" + custId;}public String getOrderById(String orderId) {// TODO Auto-generated method stubreturn "order" + orderId;}public String getSubOrderById(String subOrderId) {// TODO Auto-generated method stubreturn "subOrder" + subOrderId;}}
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;@Overrideprotected void doInit() throws ResourceException {this.customerId = (String) getRequest().getAttributes().get("custId");}@Getpublic String represent() {return "get customer id :" + orderDao.getCustomerById(customerId) ;}public void setOrderDao(OrderDao orderDao) {this.orderDao = orderDao;}}
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;@Overrideprotected void doInit() throws ResourceException {this.orderId = (String) getRequest().getAttributes().get("orderId");this.subOrderId = (String) getRequest().getAttributes().get("subOrderId");}@Getpublic 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;}}
<?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" ref="restRouter" /></bean><!-- router --><bean id="restRouter" id="customerResrouce"/><bean name="/orders/{orderId}/{subOrderId}" id="orderResrouce"/><!-- dao --><bean id="orderDao" /></beans>
<?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>
<?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" ref="restRouter" /></bean><!-- router --><bean id="restRouter" bean="customerResource" /></bean></entry><entry key="/orders/{orderId}/{subOrderId}"><bean bean="orderResource" /></bean></entry></map></property></bean><!-- resource --><bean id="customerResource" /></beans>
<?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>
<?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" id="customerResrouce"/><bean name="/orders/{orderId}/{subOrderId}" id="orderResrouce"/><!-- dao --><bean id="orderDao" /></beans>