作用: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:, 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 {
分享到:
相关推荐
你可以配置`<cxf:bus>`元素下的`<cxf:features>`或`<cxf:properties>`标签来控制日志级别和实现。 - 例如,添加`<cxf:property key="org.apache.cxf.logging.FrontendLoggerClass" value="org.apache.cxf.common....
首先,我们要理解`EndpointImpl`是CXF框架用于构建和配置Web服务端点的核心类。通过实例化这个类,我们可以自定义服务的行为,包括添加拦截器,以实现诸如日志记录、安全验证、性能统计等额外功能。 拦截器在CXF中...
首先,我们需要创建一个实现了`org.apache.cxf.interceptor.Fault`和`org.apache.cxf.phase.PhaseInterceptor`接口的类。这个类通常会包含处理权限检查的业务逻辑。例如,你可以定义一个名为`...
2. **配置Spring**:在Spring配置文件中,我们需要声明CXF的服务端点(Endpoint)和相关的Bus实例。Spring会自动管理这些组件,提供服务的生命周期控制。 ```xml xmlns:xsi=...
3. **Spring集成**:在Spring3.2中,我们可以使用`<cxf:bus>`和`<cxf:interceptor>`标签将自定义拦截器注册到CXF Bus中。这样,Spring容器会管理拦截器的生命周期,并在需要时注入其他依赖。 4. **拦截器链**:CXF...
<cxf:bus-ref>myBus</cxf:bus-ref> </cxf:client> ``` 4. **执行调用**:在Spring配置生效后,可以直接通过客户端接口进行服务调用。 **总结** Apache CXF提供了简洁的API和工具,使得发布和调用SOAP 1.2 Web...
将自定义的Interceptor添加到CXF服务端或客户端的配置中。如果是Spring配置,可以在`cxf.xml`文件中添加以下代码: ```xml <cxf:bus> <cxf:outInterceptors> </cxf:outInterceptors> </cxf:bus> ``` 4. *...
Bus 是整个 CXF 架构的核心,类似于 Spring 中的 ApplicationContext,用于管理和配置 CXF 的各种资源,例如 WSDL 管理器、绑定工厂等。Bus 提供了一个可扩展的环境,允许开发者自定义资源或替换默认资源。默认情况...
- `org.apache.cxf.interceptor.ClientInterceptor` 和 `org.apache.cxf.interceptor.ServerInterceptor`:分别用于客户端和服务端。 - `org.apache.cxf.phase.PhaseInterceptor`:通用接口,适用于任何拦截器。 ...
- 使用`Bus`或`Endpoint`的配置选项。 例如,在客户端添加一个Interceptor来记录请求和响应信息: ```java // 定义Interceptor public class LoggingInterceptor extends AbstractPhaseInterceptor<Message> { ...
#### 二、CXF环境搭建与配置 CXF的安装非常简单,只需要下载官方提供的jar包并将其加入到项目中即可。CXF官方网站为[http://cxf.apache.org/](http://cxf.apache.org/),其中提供了详细的文档和支持。 **下载地址*...
- `org.apache.cxf.interceptor.Interceptor`:处理消息的拦截器,提供了对请求和响应进行预处理和后处理的机制。 - `org.apache.cxf.frontend.ServerFactoryBean`和`ClientFactoryBean`:分别用于配置和创建服务器...
此外,还需配置CXF的Bus实例,这是CXF框架的核心组件,负责处理服务请求的路由和转换。 #### 五、服务发布与客户端调用 服务发布后,客户端可通过调用特定的URL访问服务。CXF支持多种客户端编程模型,包括JAX-WS和...
BusFactory负责创建Bus,SpringBusFactory是默认实现,它会自动加载META-INF/cxf目录下的Spring配置文件,以构建ApplicationContext。开发者可以自定义配置文件以定制Bus的行为。 **2. 消息传递和拦截器** CXF基于...
BusFactory负责创建Bus,SpringBusFactory是默认实现,它会读取META-INF/cxf目录下的配置文件来构建ApplicationContext。 2. **消息传递和拦截器**:CXF基于一个通用的消息层,由消息、拦截器和拦截器链构成。拦截...