spring通过注解自动暴露Hessian服务

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());
		}
	}

}

关于零壹视界

个人博客:http://www.xetlab.com
零壹视界

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值