spring通過注解自動暴露Hessian服務(wù)

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

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容