webService之(五)Spring与CXF整合

来源:互联网 发布:遇到网络骗局怎么办 编辑:程序博客网 时间:2024/05/22 13:45
因为Spring提供了annotation注解,所以整合Spring发布CXF WebService就更加简单了。

与之前一样,同样用maven管理

<!-- CXF -->  <dependency>      <groupId>org.apache.cxf</groupId>      <artifactId>cxf-rt-frontend-jaxws</artifactId>      <version>3.0.3</version>  </dependency> <!-- spring4 --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.0.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>4.0.0.RELEASE</version></dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.0.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.1.0.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>4.0.0.RELEASE</version></dependency>

首先新建接口HelloWorld

@WebService@Componentpublic interface HelloWorld {String sayHi(String name);}
接口实现HelloWorldImpl

public class HelloWorldImpl implements HelloWorld{@Overridepublic String sayHi(String name) {return "hello " + name;}}

spring Bean配制spring.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:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <context:component-scan base-package="com.xyj.cxf.*" />    <import resource="spring-cxf.xml" /></beans> 
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:jaxws="http://cxf.apache.org/jaxws"       xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://cxf.apache.org/jaxws       http://cxf.apache.org/schemas/jaxws.xsd">     <jaxws:endpoint id="helloWorld" implementor="com.xyj.cxf.service.impl.HelloWorldImpl" address="/spring/hello"/> </beans>

接着是web.xml文件的配置:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">   <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:spring.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <servlet>    <servlet-name>cxf</servlet-name>    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>cxf</servlet-name>    <url-pattern>/*</url-pattern>  </servlet-mapping></web-app>

然后发布工程

0 0
原创粉丝点击