59.2. 使用配置添加拦截器

概述

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

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

配置元素

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

表 59.1. 拦截器配置元素

element描述

inInterceptors

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

outInterceptors

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

inFaultInterceptors

包含配置拦截器列表,用于添加到端点的入站故障处理拦截器链。

outFaultInterceptors

包含配置拦截器列表,用于添加到端点的出站故障处理拦截器链。

所有拦截器配置元素都会取一个 列表 子元素。list 元素具有一个子项,每个拦截器都附加到链。可以使用 bean 元素直接配置拦截器或引用拦截器的 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 服务端点”