/* * Created on 2005-7-25 * */ package whao.test.hessian.server; /** * @author Hao Wei * */ public interface MyService { public String doSomething(String s);; }
/* * Created on 2005-7-25 * */ package whao.test.hessian.server.impl; import whao.test.hessian.server.MyService; /** * @author Hao Wei * */ public class MyServiceImpl implements MyService { /* (non-Javadoc); * @see whao.test.hessian.server.MyService#doSomething(java.lang.String); */ public String doSomething(String s); { return "HAHAHA: " + s; } }
<servlet> <servlet-name>myservice</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>service-class</param-name> <param-value>whao.test.hessian.server.impl.MyServiceImpl</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>myservice</servlet-name> <url-pattern>/myservice</url-pattern> </servlet-mapping>
/* * Created on 2005-7-25 * */ package whao.test.hessian.client; import whao.test.hessian.server.MyService; import com.caucho.hessian.client.HessianProxyFactory; /** * @author Hao Wei * */ public class TestMain { public static void main(String[] args); throws Exception { HessianProxyFactory proxyFactory = new HessianProxyFactory();; MyService service = (MyService); proxyFactory.create(MyService.class, "http://localhost:8080/test_web/myservice");; System.out.println(service.doSomething("xixixixi"););; System.out.println("ok!");; } }
<bean id="myService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value> http://localhost:8080/test_web/myservice </value> </property> <property name="serviceInterface"> <value>whao.test.hessian.server.MyService</value> </property> </bean>
/* * Created on 2005-7-25 * */ package whao.test.hessian.client; import whao.test.hessian.server.MyService; import whao.util.spirng.SpringBeanFactory; import com.caucho.hessian.client.HessianProxyFactory; /** * @author Hao Wei * */ public class TestMain { public static void main(String[] args); throws Exception { testWithSpring();; } public static void testWithSpring();{ MyService service = (MyService);SpringBeanFactory.getBean("myService");; System.out.println(service.doSomething("lllllllll"););; System.out.println("ok!");; } public static void testWithoutSpring(); throws Exception { HessianProxyFactory proxyFactory = new HessianProxyFactory();; MyService service = (MyService); proxyFactory.create(MyService.class, "http://localhost:8080/test_web/myservice");; System.out.println(service.doSomething("xixixixi"););; System.out.println("ok!");; } }
/* * Created on 2005-7-25 * */ package whao.util.spirng; import javax.servlet.ServletContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; /** * @author Hao Wei * */ public class SpringBeanFactory { private static final Log log = LogFactory.getLog(SpringBeanFactory.class);; private static ApplicationContext ctx = null; public static Object getBean(ServletContext context, String beanID); { log.info("beanID=" + beanID);; ApplicationContext ac = WebApplicationContextUtils .getWebApplicationContext(context);; return ac.getBean(beanID);; } public static Object getBean(String beanID);{ if(ctx == null);{ ctx = new FileSystemXmlApplicationContext( "D:\\whao-work\\src\\test_web\\test_web\\WEB-INF\\applicationContext.xml");; } return ctx.getBean(beanID);; } }