Ihello.java |
package cn.tempus.hessian;
public interface IHello {
public String sayHello(String name);
}
|
HelloImpl.java |
package cn.tempus.hessian;
import com.caucho.hessian.server.HessianServlet;
public class HelloImpl extends HessianServlet implements IHello {
private static final long serialVersionUID = -6448991611447150108L;
public String sayHello(String name) { return "Hello: "+name; }
}
|
Web.xml |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"version="3.0"> <display-name>SpringHessian</display-name>
<servlet> <servlet-name>remote</servlet-name> <!-- 使用Spring的代理Servlet --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>namespace</param-name> <param-value>remoting-servlet</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> </web-app> |
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 id="hello" class="cn.tempus.hessian.HelloImpl" />
<!-- 使用Spring的HessianServie做代理 --> <bean name="/Hello" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="hello" /> <!-- Hessian服务的接口 --> <property name="serviceInterface"value="cn.tempus.hessian.IHello" /> </bean> </beans> |
Client.java |
package cn.tempus.hessian;
import java.net.MalformedURLException;
import com.caucho.hessian.client.HessianProxyFactory;
public class Client {
public static void main(String[] args) throws MalformedURLException { // Spring Hessian代理Servelet String url = "http://localhost:8080/SpringHessian/remote/Hello"; HessianProxyFactory factory = new HessianProxyFactory(); IHello api = (IHello) factory.create(IHello.class, url);
System.out.println(api.sayHello("morris")); }
} |
控制台打印信息 |
Running Jetty 6.1.26 2014-12-29 17:07:47.843:INFO::Logging to STDERR via org.mortbay.log.StdErrLog ParentLoaderPriority enabled Context path:/SpringHessian ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\build\classes ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\com.springsource.org.aopalliance-1.0.0.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\commons-logging-1.0.4.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\hessian-4.0.37.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-aop-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-aspects-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-beans-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-context-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-context-support-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-core-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-expression-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-instrument-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-instrument-tomcat-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-jdbc-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-jms-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-messaging-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-orm-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-oxm-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-test-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-tx-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-web-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-webmvc-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-webmvc-portlet-4.1.3.RELEASE.jar ProjectClassLoader: entry=D:\Morris\javaPrj\SpringHessian\WebContent\WEB-INF\lib\spring-websocket-4.1.3.RELEASE.jar 2014-12-29 17:07:47.906:INFO::jetty-6.1.26 2014-12-29 17:07:48.453:INFO:/SpringHessian:Initializing Spring FrameworkServlet ‘remote‘ 十二月 29, 2014 5:07:48 下午org.springframework.web.servlet.FrameworkServlet initServletBean 信息: FrameworkServlet ‘remote‘: initialization started 十二月 29, 2014 5:07:48 下午org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing WebApplicationContext for namespace ‘remoting-servlet‘: startup date [Mon Dec 29 17:07:48 CST 2014]; root of context hierarchy 十二月 29, 2014 5:07:48 下午org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from ServletContext resource [/WEB-INF/remoting-servlet.xml] 十二月 29, 2014 5:07:49 下午org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler 信息: Mapped URL path [/Hello] onto handler ‘/Hello‘ 十二月 29, 2014 5:07:49 下午org.springframework.web.servlet.FrameworkServlet initServletBean 信息: FrameworkServlet ‘remote‘: initialization completed in 968 ms 2014-12-29 17:07:49.468:INFO::Started SelectChannelConnector@0.0.0.0:8080
|
结果如下:
Hello: morris
原文地址:http://blog.csdn.net/u022812849/article/details/42240743