public interface IHessianService { public String getString(String value); public Bean getBean(); }
public class Bean implements Serializable { private static final long serialVersionUID = 1L; private String value; public Bean(String value) { this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
<? xml version="1.0" encoding="UTF-8"?> <beans xmlns=" http://www.springframework.org/schema/beans " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns:aop=" http://www.springframework.org/schema/aop " xmlns:tx=" http://www.springframework.org/schema/tx " xmlns:lang=" http://www.springframework.org/schema/lang " xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd "> <bean id="hessionService" class="zh.hessian.hessianServer.HessianServiceImpl" /> <bean name="/hessianService.do" class="org.springframework.remoting.caucho. HessianServiceExporter"> <property name="service" ref="hessionService" /> <property name="serviceInterface" value="zh.hessian.hessianJar.IHessianService" /> </bean> </beans>
<web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet. DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-hessian.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
public class HessianServiceImpl implements IHessianService { @Override public String getString() { return "string"; } @Override public Bean getBean() { return new Bean("value"); } }
<? xml version="1.0" encoding="UTF-8"?> <beans xmlns=" http://www.springframework.org/schema/beans " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xmlns:context=" http://www.springframework.org/schema/context " xmlns:mvc=" http://www.springframework.org/schema/mvc " xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id="hessionServiceClient" class="org.springframework.remoting.caucho. HessianProxyFactoryBean"> <property name="serviceUrl" value=" http://localhost:8080/hessianServer/hessianService.do " /> <property name="serviceInterface" value="zh.hessian.hessianJar.IHessianService" /> </bean> </beans>
public class HessianSpringClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-client-hessian.xml"); IHessianService service = (IHessianService) context.getBean("hessionServiceClient"); System.out.println(service.getString()); System.out.println(service.getBean().getValue()); } }
getString:REQ + zhaohui getBean:value
public class HessianProxyFactoryBean extends HessianClientInterceptor implements FactoryBean<Object> { private Object serviceProxy; @Override public void afterPropertiesSet() { super.afterPropertiesSet(); this.serviceProxy = new ProxyFactory(getServiceInterface(), this).getProxy(getBeanClassLoader()); } public Object getObject() { return this.serviceProxy; } public Class<?> getObjectType() { return getServiceInterface(); } public boolean isSingleton() { return true; } }
public Object getProxy(ClassLoader classLoader) { return createAopProxy().getProxy(classLoader); }
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException { if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) { Class targetClass = config.getTargetClass(); if (targetClass == null) { throw new AopConfigException("TargetSource cannot determine target class: " + "Either an interface or a target is required for proxy creation."); } if (targetClass.isInterface()) { return new JdkDynamicAopProxy(config); } return CglibProxyFactory.createCglibProxy(config); } else { return new JdkDynamicAopProxy(config); } }
final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable { public Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource()); } Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised); findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this); } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { ...... Object retVal; ...... invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain); retVal = invocation.proceed(); ...... return retVal; } }
public Object invoke(MethodInvocation invocation) throws Throwable { if (this.hessianProxy == null) { throw new IllegalStateException("HessianClientInterceptor is not properly initialized - " + "invoke 'prepare' before attempting any operations"); } ClassLoader originalClassLoader = overrideThreadContextClassLoader(); try { return invocation.getMethod().invoke(this.hessianProxy, invocation.getArguments()); } ...... }
protected Object createHessianProxy(HessianProxyFactory proxyFactory) throws MalformedURLException { Assert.notNull(getServiceInterface(), "'serviceInterface' is required"); return proxyFactory.create(getServiceInterface(), getServiceUrl()); }
String url = " http://localhost:8080/hessianServer -0.0.1-SNAPSHOT/hessianService"; HessianProxyFactory factory = new HessianProxyFactory(); IHessianService hessianService = null; hessianService = (IHessianService) factory.create(IHessianService.class, url);
public void invoke(InputStream inputStream, OutputStream outputStream) throws Throwable { Assert.notNull(this.skeleton, "Hessian exporter has not been initialized"); doInvoke(this.skeleton, inputStream, outputStream); } protected void doInvoke(HessianSkeleton skeleton, InputStream inputStream, OutputStream outputStream) throws Throwable { ...... AbstractHessianInput in; AbstractHessianOutput out; if (code == 'H') { // Hessian 2.0 stream major = isToUse.read(); minor = isToUse.read(); if (major != 0x02) { throw new IOException("Version " + major + "." + minor + " is not understood"); } in = new Hessian2Input(isToUse); out = new Hessian2Output(osToUse); in.readCall(); } else if (code == 'C') { // Hessian 2.0 call... for some reason not handled in HessianServlet! isToUse.reset(); in = new Hessian2Input(isToUse); out = new Hessian2Output(osToUse); in.readCall(); } else if (code == 'c') { // Hessian 1.0 call major = isToUse.read(); minor = isToUse.read(); in = new HessianInput(isToUse); if (major >= 2) { out = new Hessian2Output(osToUse); } else { out = new HessianOutput(osToUse); } } ...... skeleton.invoke(in, out); ...... }