demo 下载:test-hessian.zip
1 2 3 |
public interface BasicAPI { public String hello(); } |
1 2 3 4 5 6 7 8 9 10 |
public class BasicService implements BasicAPI { private String _greeting = "Hello, javacoder.cn"; public void setGreeting(String greeting) { _greeting = greeting; } public String hello() { return _greeting; } } |
该servlet的作用是对url"/hello"的请求由"service-class"表示的类处理
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<servlet> <servlet-name>hello</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>service-class</param-name> <param-value>cn.javacoder.test.hessian.BasicService</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> |
1 2 3 4 |
String url = "http://localhost:8080/test-hessian/remote/hello"; HessianProxyFactory factory = new HessianProxyFactory(); BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url); System.out.println("hello(): " + basic.hello()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<servlet> <servlet-name>remote</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:remote-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remote</servlet-name> <url-pattern>/remote/*</url-pattern> </servlet-mapping> |
1 2 3 4 5 6 7 8 9 |
<bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter"> <!-- service引用具体的实现实体Bean --> <property name="service"> <bean name="basicService" class="cn.javacoder.test.hessian.BasicService"/> </property> <property name="serviceInterface" value="cn.javacoder.test.hessian.BasicAPI"/> </bean> |
一样的过程,添加依赖到pom.xml,配置客户端的applicationContext.xml 和调用
1 2 3 4 |
<bean id="hello" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/test-hessian/remote/hello"/> <property name="serviceInterface" value="cn.javacoder.test.hessian.BasicAPI"/> </bean> |
调用的测试代码如下
1 2 3 4 5 |
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); BasicAPI basic = applicationContext.getBean(BasicAPI.class); System.out.println("hello(): " + basic.hello()); applicationContext.close(); |
本文由javacoder.cn整理
Comments are closed.