首先你得先了解常見的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服務,從目前測試來看還不可行。