CXF2.7.3 与spring 3集成 .
1 需要的jar包
cxf包括:cxf-2.7.3.jar? wsdl4j-1.6.2.jar? xmlschema-core-2.0.3.jar? neethi-3.0.2.jar httpasyncclient-4.0-beta3.jar httpclient-4.2.1.jar httpcore-4.2.2.jar httpcore-nio-4.2.2.jar
spring包括:spring-aop-3.0.7.RELEASE.jar spring-asm-3.0.7.RELEASE.jar spring-beans-3.0.7.RELEASE.jar spring-context-3.0.7.RELEASE.jar spring-core-3.0.7.RELEASE.jar spring-expression-3.0.7.RELEASE.jar spring-jms-3.0.7.RELEASE.jar spring-tx-3.0.7.RELEASE.jar spring-web-3.0.7.RELEASE.jar
其它:commons-logging-1.1.1.jar
?
2 在web.xml填加以下配置
?<?xml version="1.0" encoding="UTF-8"?>?
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"?
??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
??? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee???
??? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">?
??? <context-param>?
??????? <param-name>contextConfigLocation</param-name>?
??????? <param-value>classpath:spring-cxf.xml</param-value>?
??? </context-param>?
?
??? <listener>?
??????? <listener-class>?
???????? org.springframework.web.context.ContextLoaderListener
??????? </listener-class>?
??? </listener>?
??????
??? <servlet>?
??????? <servlet-name>CXFServlet</servlet-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>/webservice/*</url-pattern>????
??? </servlet-mapping>?
</web-app>
?
3 創(chuàng)建服務(wù)端webservice類
1)創(chuàng)建IHelloWorld接口
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface IHelloWorld {
?public String sayHello(String name);
}
2)創(chuàng)建IHelloWorld接口的實(shí)現(xiàn)類HelloWorldImpl
import javax.jws.WebService;
public class HelloWorldImpl implements IHelloWorld {
?@Override
?public String sayHello(String name) {
??System.out.println("say hello is called");
??return "hello "+name;
?}
}
4)創(chuàng)建客戶端訪問webservice類
import org.springframework.context.ApplicationContext;
public class Client {
?public static void main(String[] args){
??ApplicationContext context = new ClassPathXmlApplicationContext("spring-cxf.xml");?
??IHelloWorld helloWorld=(IHelloWorld)context.getBean("helloWorldClient");
??System.out.println(helloWorld.sayHello("Test"));
?}
}
?
3 創(chuàng)建spring配置文件spring-cxf.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
?xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
?xmlns:jaxws="http://cxf.apache.org/jaxws"
?xsi:schemaLocation="http://www.springframework.org/schema/beans
?http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
?http://www.springframework.org/schema/tx
?http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
?http://www.springframework.org/schema/aop
?http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
??? http://www.springframework.org/schema/context
?http://www.springframework.org/schema/context/spring-context-3.0.xsd
?http://cxf.apache.org/jaxws
?http://cxf.apache.org/schemas/jaxws.xsd"
?????????? <!--CXF配置-->
????????? <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"/>
????????
?????????? <!--服務(wù)端發(fā)布的webservice-->
?????????? <jaxws:endpoint id="helloWorld" implementor="openProjectTest.cxf.HelloWorldImpl" address="/HelloWorld"/>
?
?????????? <!--客戶端調(diào)用的webservice-->
?????????? <jaxws:client id="helloWorldClient" address="http://localhost:8080/web工程名/webservice/HelloWorld" serviceClass="openProjectTest.cxf.IHelloWorld"/>
</beans>
?
4 測試
1)訪問以下地址驗(yàn)證服務(wù)端是否配制成功
http://localhost:8080/web工程名/webservice/HelloWorld?wsdl? 如果可看到wsdl的xml文件則成功
http://localhost:8080/web工程名/webservice? 直接訪問該地址可查看系統(tǒng)提供幾個(gè)webservice服務(wù)
2)運(yùn)行Client類輸出 hello Test 則客戶端連接成功
總結(jié)
以上是生活随笔為你收集整理的CXF2.7.3 与spring 3集成 .的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AspectJ切入点语法详解
- 下一篇: Eclipse中修改tomcat内存大小