基于Spring Boot的全注解版Hessian服务

首先你得先了解常见的Hessian的spring配置方式,即XML配置方式,不知道的可参考一下网上的博客,如:java

1. Spring4整合Hessian4(MavenWeb实例)web

2. hessian系列之三:与Spring集成spring

如今咱们将基于spring boot 来全注解配置Hessian。app

1、将Hessian服务Bean转换为注解方式框架

A. xml版本hessian-server.xml:ide

    <bean id="sayHelloServiceImpl" class="com.etak.services.SayHelloServiceImpl"></bean>工具

    <bean name="/sayHello" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="sayHelloServiceImpl" />
        <property name="serviceInterface" value="com.etak.services.SayHelloService" />
    </bean>测试

B. 注解版则以下:url

@Service("sayHelloService")spa

public class SayHelloServiceImpl implements SayHelloService {

 

    @Override

    public String sayHello(String name) {

        System.out.println("服务端方法被调用!");

        return  "Hello,"+name+"!";

    }

}

2、将web.xml配置Servlet方式配置为直接暴露服务方式

A. 原来的web.xml方式:

<servlet>
    <servlet-name>hessianServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:server/hessian-server.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>hessianServlet</servlet-name>
    <url-pattern>/hessian/*</url-pattern>
</servlet-mapping>

B. 暴露服务Bean方式

能够直接在Application.java类中定义Bean

@Autowired
private SayHelloService sayHelloService;

@Bean(name = "/HelloService")
public HessianServiceExporter exportHelloService() {
    HessianServiceExporter exporter = new HessianServiceExporter();
    exporter.setService(sayHelloService);
    exporter.setServiceInterface(SayHelloService.class);
    return exporter;
}

注意理解

1.HessianServiceExporter是由Spring.web框架提供的Hessian工具类,可以将bean转化为Hessian服务

2.@Bean(name = "/HelloService")加斜杠方式会被spring暴露服务,看日志:

 [main] INFO  o.s.w.s.h.BeanNameUrlHandlerMapping - Mapped URL path [/HelloService] onto handler '/HelloService'

 3. 想经过注解ServletRegistrationBean来代替DispatcherServlet,从而暴露Hessian服务,从目前测试来看还不可行。

相关文章
相关标签/搜索