59.2. 使用配置添加拦截器

概述

将拦截器附加到端点的最简单方法是使用 配置文件。要附加到端点的每个拦截器都使用标准 Spring bean 进行配置。然后,拦截器 bean 可以使用 Apache CXF 配置元素添加到正确的拦截器链中。

具有关联拦截器链的每个运行时组件均可使用专用的 Spring 元素进行配置。每个组件的元素都有一组标准的子项,用于指定其拦截器链。每个与组件关联的拦截器链都有一个子项。拦截器的子项列表添加到链中。

配置元素

表 59.1 “拦截器链配置元素” 描述将拦截器附加到运行时组件的四个配置元素。

表 59.1. 拦截器链配置元素

元素描述

inInterceptors

包含 Bean 配置拦截器列表,以添加到端点的入站拦截器链中。

outInterceptors

包含 Bean 配置拦截器列表,以添加到端点的出站拦截器链中。

inFaultInterceptors

包含 Bean 配置拦截器的列表,以添加到端点的入站错误处理拦截器链中。

outFaultInterceptors

包含 Bean 配置拦截器列表,以添加到端点的出站错误处理拦截器链中。

所有拦截器链配置元素都取一个 列表 子元素。list 元素有一个子项,用于附加到链的每个拦截器。拦截器可以通过直接配置拦截器或引用配置拦截器的 bean 元素来指定。

例子

例 59.1 “将拦截器附加到总线” 显示将拦截器附加到总线入站拦截器链的配置。

例 59.1. 将拦截器附加到总线

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cxf="http://cxf.apache.org/core"
       xmlns:http="http://cxf.apache.org/transports/http/configuration"
       xsi:schemaLocation="
       http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
       http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  ...
  <bean id="GZIPStream" class="demo.stream.interceptor.StreamInterceptor"/>

  <cxf:bus>
    *<cxf:inInterceptors>
      <list>
        <ref bean="GZIPStream"/>
      </list>
    </cxf:inInterceptors>*
  </cxf:bus>
</beans>

例 59.2 “将拦截器附加到 JAX-WS 服务提供商” 显示将拦截器附加到 JAX-WS 服务的出站拦截器链的配置。

例 59.2. 将拦截器附加到 JAX-WS 服务提供商

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:wsa="http://cxf.apache.org/ws/addressing"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

  <jaxws:endpoint ...>
    *<jaxws:outInterceptors>
      <list>
        <bean id="GZIPStream" class="demo.stream.interceptor.StreamInterceptor" />
      </list>
    </jaxws:outInterceptors>*
  </jaxws:endpoint>
</beans>

更多信息

有关使用 Spring 配置配置端点的更多信息,请参阅 第 IV 部分 “配置 Web 服务端点”