remoting服务例子
1、remoting-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//Spring//DTD Bean//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans><bean name="/hello/find" ref="helloImpl" /><property name="serviceInterface" value="com.IHello" /></bean></beans>
备注:该文件必须放在“WEB-INF/”下面。
2、remoting-client.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//Spring//DTD Bean//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans><bean name="testRemote" value="http://localhost:8080/Test/remoting/hello/find" /><property name="serviceInterface" value="com.inf.IHello" /></bean></beans>
3、web.xml配置
<servlet><servlet-name>remoting</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>2</load-on-startup></servlet><servlet-mapping><servlet-name>remoting</servlet-name><url-pattern>/remoting/*</url-pattern></servlet-mapping>
4、调用方法
方法一:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");IHello hello = (IHello)context.getBean("testRemote");Result result = hello.findByName("jack");System.out.println(result.getLstUser().size());备注:基于“remoting-client.xml”的配置
方法二:
//免配置HttpInvokerProxyFactoryBean bean=new HttpInvokerProxyFactoryBean();bean.setServiceUrl("http://localhost:8080/Test/remoting/hello/find");bean.setServiceInterface(IHello.class);bean.afterPropertiesSet();//必须IHello hello = (IHello)bean.getObject();Result result = hello.findByName("jack");System.out.println(result.getLstUser().size());5、优缺点和注意事项
1.优缺点
优点:效率高,几乎跟调用本地接口一样
缺点:不跨平台
2.注意事项
remoting服务 正好与 rest服务 互补,在参数传递和返回值上完全没有限制,能满足java用户的全部需求。
该文章基于上篇文章(rest服务例子:http://hdxiong.iteye.com/admin/blogs/998069)