文章出處

作用:BUS是cxf的支架,它主要擔當擴展及攔截器提供者的角色。

 

在這里主要講講 bus的interceptor的功能

目前配置cxf的interceptor主要有2中方法:

1、通過xml配置文件的方法,使用<cxf:bus>
2、通過在java代碼中使用編碼的方式來添加攔截器

 

下面來看2個例子

1 配置文件方式配置 cxf bus interceptor

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:cxf="http://cxf.apache.org/core"
      xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="logOutbound" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>

    <cxf:bus>
        <cxf:outInterceptors>
            <ref bean="logOutbound"/>
        </cxf:outInterceptors>
    </cxf:bus>
</beans>

 


注意:在使用<cxf:bus>時候,一定要引入 命名空間xmlns:cxf=http://cxf.apache.org/core,及其對應的模式http://cxf.apache.org/schemas/core.xsd
2、java代碼硬編碼方式添加攔截器

 在服務端添加攔截

import javax.xml.ws.Endpoint;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;

Object implementor = new GreeterImpl();
EndpointImpl ep = (EndpointImpl) Endpoint.publish("http://localhost/service", implementor);

ep.getServiceFactory().getBus().getInInterceptors().add(new LoggingInInterceptor());
ep.getServiceFactory().getBus().getOutInterceptors().add(new LoggingOutInterceptor());

 

在客戶端添加攔截

 

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

public class WSClient {
    public static void main (String[] args) {
        MyService ws = new MyService();
        MyPortType port = ws.getPort();
       
        Client client = ClientProxy.getClient(port);
        client.getInInterceptors().add(new LoggingInInterceptor());
        client.getOutInterceptors().add(new LoggingOutInterceptor()); 

 

 

另外在java代碼中也可以通過使用注解的方式添加攔截器

在SEI及SEI的實現類中添加注解有著不同的含義,若在SEI中添加攔截器注解,則表示其會在client及server端都起作用,若在SEI的實現類中添加注解,則只會在server端起作用,例子:

SEI 實現類

@javax.jws.WebService(portName = "MyWebServicePort", serviceName = "MyWebService", ...)
@Features(features = "org.apache.cxf.feature.LoggingFeature")       
public class MyWebServicePortTypeImpl implements MyWebServicePortType {

 

 

效果等同于如下代碼

 

import org.apache.cxf.interceptor.InInterceptors;
import org.apache.cxf.interceptor.OutInterceptors;

@javax.jws.WebService(portName = "WebServicePort", serviceName = "WebServiceService", ...)
@InInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingInInterceptor")
@OutInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingOutInterceptor")
public class WebServicePortTypeImpl implements WebServicePortType {

 

 

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()