Hessian與spring集成
Hessian可以與spring完美無縫的集成,我們先來看一下如何集成:
- 服務(wù)端服務(wù)暴露
@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服務(wù):http://localhost:8080/Hello/helloService
- 客戶端聲明與調(diào)用
聲明
<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>
調(diào)用
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/ApplicationContext.xml");
HelloService helloService = (HelloService) context.getBean("helloService");
helloService.sayHi("hi, i am client");
存在的問題
上述的方法是最常用的集成方式,但存在一個問題,如果我們現(xiàn)在要在服務(wù)端暴露另外一個服務(wù),那么需要添加如下代碼:
@Autowired
private AnotherService1 anotherService;
@Bean(name = "/anotherService")
public HessianServiceExporter exportAnotherService() {
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(anotherService);
exporter.setServiceInterface(AnotherService .class);
return exporter;
}
如果再來一個服務(wù)呢,那我們就再添加類似的代碼,能不能不重復(fù)添加這些類似的暴露代碼呢?
使用注解自動暴露服務(wù)
首先,新建一個叫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注解的服務(wù)類換成@HessianService
@HessianService
public class HelloServiceImpl implements HelloService {
...
...
}
然后,我們使用spring的BeanFactoryPostProcessor機制,動態(tài)暴露hessian服務(wù)
@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());
}
}
}