CXF結合spring的配置和開發流程1

1、引入需要的jar包http://cxf.apache.org/download.html 可以下載相應jar包,本次使用的是apache-cxf-2.2.12

2、 web.xml 中配置 cxf

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/classes/applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<display-name>CXF Servlet</display-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

3、編寫applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
	<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"






	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">







	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />







	<jaxws:endpoint id="helloWorld"
		implementor="com.fxb.webservice.service.impl.HelloServiceImpl"
		address="/HelloWorld">
		<jaxws:inInterceptors>
			<bean class="com.fxb.webservice.interceptor.MyInterceptor"></bean>
		</jaxws:inInterceptors>
	</jaxws:endpoint>

</beans>
	<!-- END SNIPPET: beans -->

 4、然後就可以編寫 service 代碼,作爲服務層的接口必須加上 @WebService 標註,因爲這個接口將會被我們暴露爲 webservice

package com.fxb.webservice.service;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService






public interface HelloService {

	public String hello(@WebParam(name="name")String name);
}

5、編寫 application-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

    <bean id="client" class="com.fxb.webservice.client.HelloClient" 
      factory-bean="clientFactory" factory-method="create"/>
    
	<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
	  <property name="serviceClass" value="com.fxb.webservice.service.HelloService"/>
	  <property name="address" value="http://localhost:8080/CXFSpring/HelloWorld"/>
	</bean>
	  
</beans>
<!-- END SNIPPET: beans -->

6、編寫客戶端類

package com.fxb.webservice.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fxb.webservice.service.HelloService;

public class HelloClient {
    public static void main(String[] args) {
	    // START SNIPPET: client
        ClassPathXmlApplicationContext context 
            = new ClassPathXmlApplicationContext(new String[] {"com/fxb/webservice/client/applicationContext-client.xml"});

        HelloService client = (HelloService)context.getBean("client");

        String response = client.hello("Joe");
        System.out.println("Response: " + response);
        System.exit(0);
        // END SNIPPET: client
    }
}

7、自定的 Interceptor 一定要實現 CXF Interceptor 接口,這個接口中有兩個方法: 

 void handleFault(T message)

          當攔截消息處理失敗時候所調用的方法。

 void handleMessage(T message)

          攔截了一個消息,並做處理的方法。

package com.fxb.webservice.interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.log4j.Logger;

public class MyInterceptor extends AbstractPhaseInterceptor<Message> {
	private Logger log = Logger.getLogger(MyInterceptor.class);

	public MyInterceptor(String phase) {
		super(phase);
	}

	public MyInterceptor() {
		super(Phase.RECEIVE);
	}

	@SuppressWarnings("static-access")
	public void handleMessage(Message msg) throws Fault {
		String uri = String.valueOf(msg.get(msg.REQUEST_URI));
		StringBuffer bf = new StringBuffer(uri + ".xml");
		msg.put(msg.REQUEST_URI, String.valueOf(bf));
		System.out.println("in my intercetptor11111111111111111111");
		System.out.println(msg.get(msg.REQUEST_URI));
		log.info("in my interceptor:");

	}

	@Override
	public void handleFault(Message message) {
		super.handleFault(message);
		System.out.println("handleFault@@@@@@@@@@@@@@@@@@@@@2");
	}

}
 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章