原来的配置
<bean name="/UserService" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="serviceInterface" value="com.snailteam.service.UserService"></property> <property name="service" ref="userServiceImp"></property> </bean>
<bean class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/api/service/UserService"></property> <property name="serviceInterface" value="com.snailteam.rpc.service.UserService"></property> </bean>
现在的配置
<bean class="org.springframework.remoting.caucho.HessianExporterMapperScannerConfigurer"> <property name="basePackage" value="com.snailteam.service" /> </bean>
<bean class="org.springframework.remoting.caucho.HessianProxyMapperScannerConfigurer"> <property name="serviceUrl" value="http://localhost:8080/api/service"></property> <property name="debug" value="false"></property> <property name="connectTimeout" value="3000"></property> <property name="readTimeout" value="30000"></property> <property name="basePackage" value="com.snailteam.rpc" /> </bean>
代码
/** * @author xiaofancn@gmail.com * */ package org.springframework.remoting.caucho; import java.io.IOException; import java.lang.annotation.Annotation; import java.util.HashMap; import java.util.Map; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.core.type.filter.AssignableTypeFilter; import org.springframework.core.type.filter.TypeFilter; import org.springframework.util.StringUtils; import org.springframework.web.context.support.XmlWebApplicationContext; /** * @author xiaofancn@gmail.com * @see Mybatis-spring MapperScannerConfigurer */ public class HessianExporterMapperScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware { protected final Logger logger = LoggerFactory.getLogger(getClass()); private String serviceUrl; private String basePackage; private boolean debug; private ApplicationContext applicationContext; private Class<? extends Annotation> annotationClass; private Class<?> markerInterface; private String beanName; private Map<String, String> implClassContextName = new HashMap<String, String>(); public void setDebug(boolean debug) { this.debug = debug; } public void setImplClassContextName(Map<String, String> implClassContextName) { this.implClassContextName = implClassContextName; } public Map<String, String> getImplClassContextName() { return implClassContextName; } public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } public void setBasePackage(String basePackage) { this.basePackage = basePackage; } public void setServiceUrl(String serviceUrl) { this.serviceUrl = serviceUrl; } public void setAnnotationClass(Class<? extends Annotation> annotationClass) { this.annotationClass = annotationClass; } public void setMarkerInterface(Class<?> markerInterface) { this.markerInterface = markerInterface; } @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { } @Override public void postProcessBeanDefinitionRegistry( BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException { Scanner scanner = new Scanner(beanDefinitionRegistry); scanner.setResourceLoader(this.applicationContext); scanner.setBeanNameGenerator(new BeanNameGenerator() { @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { String beanName = definition.getBeanClassName().substring( definition.getBeanClassName().lastIndexOf(".") + 1); return "/" + beanName; } }); scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS)); } private final class Scanner extends ClassPathBeanDefinitionScanner { public Scanner(BeanDefinitionRegistry registry) { super(registry); } protected Set<BeanDefinitionHolder> doScan(String... basePackages) { Set<BeanDefinitionHolder> beanDefinitions = super .doScan(basePackages); if (beanDefinitions.isEmpty()) { logger.warn("No R mapper was found in '" + HessianExporterMapperScannerConfigurer.this.basePackage + "' package. Please check your configuration."); } else { for (BeanDefinitionHolder holder : beanDefinitions) { GenericBeanDefinition definition = (GenericBeanDefinition) holder .getBeanDefinition(); if (logger.isDebugEnabled()) { logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface"); } /** */ try { //解析扫描的类 Class cls = definition.resolveBeanClass(this.getResourceLoader().getClassLoader()); definition.getPropertyValues().add("serviceInterface", definition.getBeanClassName()); //通过class type,在spring root applaction获取对应的bean名字,取第一个。 definition.getPropertyValues().add("service",new RuntimeBeanReference(applicationContext.getParent().getBeanNamesForType(cls)[0])); definition.setBeanClass(HessianServiceExporter.class); } catch (ClassNotFoundException e) { logger.error(e); } } } return beanDefinitions; } @Override protected boolean isCandidateComponent( AnnotatedBeanDefinition beanDefinition) { return (beanDefinition.getMetadata().isInterface() && beanDefinition .getMetadata().isIndependent()); } @Override protected boolean checkCandidate(String beanName, BeanDefinition beanDefinition) throws IllegalStateException { if (super.checkCandidate(beanName, beanDefinition)) { return true; } else { logger.warn("Skipping HessianProxyFactoryBean with name '" + beanName + "' and '" + beanDefinition.getBeanClassName() + "' mapperInterface" + ". Bean already defined with the same name!"); return false; } } @Override protected void registerDefaultFilters() { boolean acceptAllInterfaces = true; // if specified, use the given annotation and / or marker interface if (HessianExporterMapperScannerConfigurer.this.annotationClass != null) { addIncludeFilter(new AnnotationTypeFilter( HessianExporterMapperScannerConfigurer.this.annotationClass)); acceptAllInterfaces = false; } // override AssignableTypeFilter to ignore matches on the actual // marker interface if (HessianExporterMapperScannerConfigurer.this.markerInterface != null) { addIncludeFilter(new AssignableTypeFilter( HessianExporterMapperScannerConfigurer.this.markerInterface) { @Override protected boolean matchClassName(String className) { return false; } }); acceptAllInterfaces = false; } if (acceptAllInterfaces) { // default include filter that accepts all classes addIncludeFilter(new TypeFilter() { public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { return true; } }); } // exclude package-info.java addExcludeFilter(new TypeFilter() { public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { String className = metadataReader.getClassMetadata() .getClassName(); return className.endsWith("package-info"); } }); } } @Override public void setBeanName(String name) { } @Override public void afterPropertiesSet() throws Exception { } }
package org.springframework.remoting.caucho; import static org.springframework.util.Assert.notNull; import java.io.IOException; import java.lang.annotation.Annotation; import java.util.Set; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.beans.factory.support.GenericBeanDefinition; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.core.type.filter.AssignableTypeFilter; import org.springframework.core.type.filter.TypeFilter; import org.springframework.remoting.caucho.HessianProxyFactoryBean; import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** * @author xiaofancn@gmail.com * @see Mybatis-spring MapperScannerConfigurer */ public class HessianProxyMapperScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware { private String serviceUrl; private String basePackage; private boolean debug; private Integer connectTimeout; private Integer readTimeout; private ApplicationContext applicationContext; private Class<? extends Annotation> annotationClass; private Class<?> markerInterface; private String beanName; public void setConnectTimeout(Integer connectTimeout) { this.connectTimeout = connectTimeout; } public void setDebug(boolean debug) { this.debug = debug; } public void setReadTimeout(Integer readTimeout) { this.readTimeout = readTimeout; } public void setApplicationContext(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } public void setBasePackage(String basePackage) { this.basePackage = basePackage; } public void setServiceUrl(String serviceUrl) { this.serviceUrl = serviceUrl; } public void setAnnotationClass(Class<? extends Annotation> annotationClass) { this.annotationClass = annotationClass; } public void setMarkerInterface(Class<?> markerInterface) { this.markerInterface = markerInterface; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException { Scanner scanner = new Scanner(beanDefinitionRegistry); scanner.setResourceLoader(this.applicationContext); scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS)); } /** * org.springframework.remoting.caucho.HessianProxyFactoryBean */ private final class Scanner extends ClassPathBeanDefinitionScanner { public Scanner(BeanDefinitionRegistry registry) { super(registry); } protected Set<BeanDefinitionHolder> doScan(String... basePackages) { Set<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages); if (beanDefinitions.isEmpty()) { logger.warn("No R mapper was found in '" + HessianProxyMapperScannerConfigurer.this.basePackage + "' package. Please check your configuration."); } else { for (BeanDefinitionHolder holder : beanDefinitions) { GenericBeanDefinition definition = (GenericBeanDefinition) holder.getBeanDefinition(); if (logger.isDebugEnabled()) { logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface"); } /** <bean class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/api/service/UserService"></property> <property name="serviceInterface" value="com.snailteam.rpc.service.UserService"></property> </bean> */ definition.getPropertyValues().add("serviceInterface", definition.getBeanClassName()); String beanName = definition.getBeanClassName().substring(definition.getBeanClassName().lastIndexOf(".")+1); Assert.notNull(serviceUrl, "HessianProxyMapperScannerConfigurer serviceUrl must not be null"); definition.setBeanClass(HessianProxyFactoryBean.class); definition.getPropertyValues().add("serviceUrl", HessianProxyMapperScannerConfigurer.this.serviceUrl+"/"+beanName); definition.getPropertyValues().add("debug", HessianProxyMapperScannerConfigurer.this.debug); definition.getPropertyValues().add("readTimeout", HessianProxyMapperScannerConfigurer.this.readTimeout); // serviceInterface } } return beanDefinitions; } @Override protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) { return (beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent()); } @Override protected boolean checkCandidate(String beanName, BeanDefinition beanDefinition) throws IllegalStateException { if (super.checkCandidate(beanName, beanDefinition)) { return true; } else { logger.warn("Skipping HessianProxyFactoryBean with name '" + beanName + "' and '" + beanDefinition.getBeanClassName() + "' mapperInterface" + ". Bean already defined with the same name!"); return false; } } @Override protected void registerDefaultFilters() { boolean acceptAllInterfaces = true; // if specified, use the given annotation and / or marker interface if (HessianProxyMapperScannerConfigurer.this.annotationClass != null) { addIncludeFilter(new AnnotationTypeFilter(HessianProxyMapperScannerConfigurer.this.annotationClass)); acceptAllInterfaces = false; } // override AssignableTypeFilter to ignore matches on the actual // marker interface if (HessianProxyMapperScannerConfigurer.this.markerInterface != null) { addIncludeFilter(new AssignableTypeFilter(HessianProxyMapperScannerConfigurer.this.markerInterface) { @Override protected boolean matchClassName(String className) { return false; } }); acceptAllInterfaces = false; } if (acceptAllInterfaces) { // default include filter that accepts all classes addIncludeFilter(new TypeFilter() { public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { return true; } }); } // exclude package-info.java addExcludeFilter(new TypeFilter() { public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { String className = metadataReader.getClassMetadata().getClassName(); return className.endsWith("package-info"); } }); } } @Override public void setBeanName(String name) { this.beanName = name; } @Override public void afterPropertiesSet() throws Exception { notNull(this.basePackage, "Property 'basePackage' is required"); } }
package org.springframework.remoting.caucho; /** * @author xiaofancn@gmail.com * @version 创建时间:2017-2-14 下午6:18:59 * 类说明 */ import java.lang.reflect.Field; import org.springframework.aop.framework.AdvisedSupport; import org.springframework.aop.framework.AopProxy; import org.springframework.aop.support.AopUtils; public class AopTargetUtils { /** * 获取 目标对象 * * @param proxy * 代理对象 * @return * @throws Exception */ public static Object getTarget(Object proxy) throws Exception { if (!AopUtils.isAopProxy(proxy)) { return proxy;// 不是代理对象 } if (AopUtils.isJdkDynamicProxy(proxy)) { return getJdkDynamicProxyTargetObject(proxy); } else { // cglib return getCglibProxyTargetObject(proxy); } } private static Object getCglibProxyTargetObject(Object proxy) throws Exception { Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0"); h.setAccessible(true); Object dynamicAdvisedInterceptor = h.get(proxy); Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised"); advised.setAccessible(true); Object target = ((AdvisedSupport) advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget(); return target; } private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception { Field h = proxy.getClass().getSuperclass().getDeclaredField("h"); h.setAccessible(true); AopProxy aopProxy = (AopProxy) h.get(proxy); Field advised = aopProxy.getClass().getDeclaredField("advised"); advised.setAccessible(true); Object target = ((AdvisedSupport) advised.get(aopProxy)).getTargetSource().getTarget(); return target; } }
相关推荐
通过以上步骤,我们完成了Spring集成Hessian的基本配置。这种方式使得服务提供者和消费者可以跨越不同的网络环境,进行高效的数据交换,极大地提升了系统的可扩展性和可维护性。在实际项目中,还需要考虑安全性、...
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。参考文档地址...
在Spring框架中集成Hessian是为了实现远程方法调用(Remote Method Invocation, RMI),这是一种轻量级的序列化协议,可以高效地传输Java对象。Hessian使得服务提供者和服务消费者之间能够通过网络进行快速的数据...
在Spring框架中集成Hessian,我们需要进行以下步骤: 1. **创建服务接口和服务实现**: 首先,你需要定义一个服务接口,然后实现这个接口。例如,我们有一个`HelloHessianService`接口,包含一个`sayHello`方法。 `...
Spring框架提供了强大的依赖注入和AOP(面向切面编程)能力,可以帮助开发者轻松地集成各种服务,包括Hessian。在Spring中整合Hessian,首先需要在Spring配置文件中定义Hessian的服务和客户端bean。 1. **Hessian...
2. **配置Hessian服务**:在Spring配置文件中,配置Hessian的bean,指定服务接口和实现类。 3. **定义AOP切面**:创建一个切面类,定义需要在Hessian调用前后执行的通知,如记录调用日志、性能统计等。 4. **配置...
在IT行业中,Hessian和Spring的集成是服务端开发中常用的一种技术,它允许我们将Java对象作为远程服务进行调用,极大地提高了开发效率和系统的可扩展性。下面将详细讲解Hessian与Spring集成的关键知识点。 首先,...
在这个实例中,我们学习了如何利用Spring的自动装配和Hessian的高效通讯能力,实现了一个简单的Web Service。这种方式适用于需要快速、轻量级交互的场景,特别适合于内部服务之间的通信,或者对性能有较高要求的应用...
1.名称:Spring + Hessian + Spring MVC(包括Server和Client).zip 2.来源:自己实现 3.备注:项目有两个系统,包括Server端和Client端,项目已经跑通,可以实现。 4.注意:client中pom.xml里最后的一个jar包,需要...
使用eclipse maven工程搭建hessian远程服务demo 分服务端的整合和客户端 建议阅读相关博客http://blog.csdn.net/heisemuyangquan/article/details/79460528
【标题】:“Spring + Hessian + Maven 整合”是一个基于Java的分布式服务...通过这个整合,开发者可以学习如何在实际项目中利用Spring的灵活性,Hessian的高效通信,以及Maven的自动化构建,来构建和管理分布式系统。
一个Spring整合Hessian的Demo,同时包含Hessian服务端与客户端。是一个Maven工程,IDE使用的Eclipse,运行前需要安装Eclipse的Maven插件。可以结合文章一起学习,地址是...
要在Spring Web Flow项目中集成Hessian服务,我们需要以下步骤: 1. **配置Hessian服务**:在服务器端,我们需要创建一个实现了所需接口的业务服务类,并在Spring配置文件中声明它为Hessian服务,使用`<bean>`标签...
【描述】中提到的"里面有具体的实例程序",意味着这个压缩包可能包含了一个完整的示例项目,该项目展示了如何配置和运行Spring与Hessian的集成,以及如何在Tomcat这样的Servlet容器中部署。这通常包括以下步骤: 1....
这些服务通常继承自Spring的`HessianServiceExporter`,这样Spring就会自动创建一个Hessian服务端点。 2. **创建Hessian Proxy**:在Flex客户端,我们需要创建一个Hessian代理来连接到服务。这通常通过Adobe的`flex...
首先,我们需要创建一个Maven项目,配置pom.xml文件来管理Spring MVC、Hessian和其它相关依赖。在pom.xml中,添加以下依赖: ```xml <groupId>org.springframework <artifactId>spring-webmvc <version>5.x.x...
Spring框架为了提供远程服务调用的支持,将其集成到自己的服务中,使得开发者能够利用Hessian的特性来实现高效、轻量级的服务间通信。本文将深入探讨Hessian在Spring中的应用以及相关研究。 一、Hessian简介 ...
1. **配置Spring**:在Spring的配置文件中定义Hessian服务的bean,通过`<bean>`标签声明服务接口和其实现类,使用`hessian-proxy-factory-bean`来创建Hessian服务的代理。 2. **暴露Hessian服务**:通过Spring的`...
本示例着重介绍如何在Spring和SpringMVC框架中集成Hessian RPC,以实现客户端和服务端的通信。 Hessian是一种轻量级的二进制Web服务协议,由Caucho公司开发。它具有较高的传输效率,支持Java和.NET等多种语言,特别...