一、Hessian与spring集成
Hessian可以与spring完美无缝的集成,我们先来看一下如何集成:
服务端服务暴露
@Autowired
private HelloService helloService;
@Bean(name = "/helloService")
public HessianServiceExporter exportHelloService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(helloService);
exporter.setServiceInterface(HelloService .class);
return exporter;
}
以上使用spring在代码配置声明bean的方式暴露了一个hession服务:http://localhost:8080/Hello/helloService
客户端声明与调用
声明
<bean id="studentService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!-- 请求代理Servlet路径 -->
<property name="serviceUrl">
<value>http://localhost:8080/Hello/helloService</value>
</property>
<!-- 接口定义 -->
<property name="serviceInterface">
<value>com.test.hello.service.HelloService</value>
</property>
</bean>
调用
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/ApplicationContext.xml");
HelloService helloService = (HelloService) context.getBean("helloService");
helloService.sayHi("hi, i am client");
二、存在的问题
上述的方法是最常用的集成方式,但存在一个问题,如果我们现在要在服务端暴露另外一个服务,那么需要添加如下代码:
@Autowired
private AnotherService1 anotherService;
@Bean(name = "/anotherService")
public HessianServiceExporter exportAnotherService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(anotherService);
exporter.setServiceInterface(AnotherService .class);
return exporter;
}
如果再来一个服务呢,那我们就再添加类似的代码,能不能不重复添加这些类似的暴露代码呢?
三、使用注解自动暴露服务
首先,新建一个叫HessianService的注解类,注意这个注解类包含了spring的Service注解
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Service;
@Target({ java.lang.annotation.ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Service
public @interface HessianService {
public abstract String value() default "";
}
接着,我们把原先用@Service注解的服务类换成@HessianService
@HessianService
public class HelloServiceImpl implements HelloService {
...
...
}
然后,我们使用spring的BeanFactoryPostProcessor机制,动态暴露hessian服务
@Component
public class HessianServiceScanner implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
String[] beanNames = beanFactory
.getBeanNamesForAnnotation(HessianService.class);
for (String beanName : beanNames) {
String className = beanFactory.getBeanDefinition(beanName)
.getBeanClassName();
Class<?> clasz = null;
try {
clasz = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new BeanInitializationException(e.getMessage(), e);
}
String hessianServiceBeanName = "/" + beanName.replace("Impl", "");
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.rootBeanDefinition(HessianServiceExporter.class);
builder.addPropertyReference("service", beanName);
builder.addPropertyValue("serviceInterface",
clasz.getInterfaces()[0].getName());
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(
hessianServiceBeanName, builder.getBeanDefinition());
}
}
}
分享到:
相关推荐
c#实现的hessian的服务端和客户端,iis亲测可以通过,
hessian 服务器 客户端 demo
整合 Dubbo + SpringMvc 4.3.1 的服务器maven工程样例,使用注解方式定义服务及IOC装配,采用Hessian协议传输 三个工程,在 cnetAdmin 和 cnetBlu 中,要引用 cnetCommon 工程。
使用eclipse maven工程搭建hessian远程服务demo 分服务端的整合和客户端 建议阅读相关博客http://blog.csdn.net/heisemuyangquan/article/details/79460528
使用hessian进行服务器和客户端通讯,此案例包括服务器和java客户端,参考博客:http://blog.csdn.net/tan313/article/details/49536163
Hessian多个版本下载,包括Hessian3.1.6,Hessian3.2.1,Hessian4.0.7
该案例有hessian java python,该案例有hessian java python,该案例有hessian java python
基于java实现hessian进行服务器之间数据交互demo项目 实现功能: 1.基于spring 2.5.6+hessian3.1.6带有签名安全机制 2.基于servlet代理机制实现HessianServlet,进行简单IP地址校验功能!
此代码展示了如何使用 springMVC 搭建 hessian 服务和 Controller 服务,里面还有如何使用的 readme 文件,告诉你如何使用它们。
Hessian一般是通过Web应用来提供服务,因此非常类似于平时我们用的 WebService。只是它不使用SOAP协议,但相比webservice而言更简单、快捷。这个文件可以是hessian在spring项目和web项目的简单配置
Hessian实例 网站地址:http://hessian.caucho.com/#Java可以下载Hessian需要的jar包 2010.11.11上传
hessian源码
使用hessian进行两台服务器资源之间同步实例,里面是源代码加使用说明
hessian资料3hessian资料3hessian资料3hessian资料3hessian资料3
引入该工程,在接口上添加@hessianService注解,在client定义url,在client端的xml里面配置hessianClientBuilder,在server里面配置hessianServerBuilder,并实例化接口。在client端里面使用接口并注入即可。
Hessian服务端 入门程序
android端使用hessian跟web服务器通讯,导入即可运行,有问题请在博客留言。http://blog.csdn.net/tan313/article/details/49536891
NULL 博文链接:https://lib.iteye.com/blog/392438
NULL 博文链接:https://paopaochao3.iteye.com/blog/550771
赠送jar包:hessian-3.3.6.jar; 赠送原API文档:hessian-3.3.6-javadoc.jar; 赠送源代码:hessian-3.3.6-sources.jar; 赠送Maven依赖信息文件:hessian-3.3.6.pom; 包含翻译后的API文档:hessian-3.3.6-javadoc-...