在spring中集成webservice 框架 CXF

首先 构建环境 

1.在eclipse下新建个web项目(我比较喜欢用eclipse),接着添加CXF必须依赖的jar包 

commons-logging-1.1.jar 
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar) 
geronimo-annotation_1.0_spec-1.1.jar (JSR 250) 
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar) 
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar) 
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181) 
jaxb-api-2.0.jar 
jaxb-impl-2.0.5.jar 
jaxws-api-2.0.jar 
neethi-2.0.jar 
saaj-api-1.3.jar 
saaj-impl-1.3.jar 
stax-api-1.0.1.jar 
wsdl4j-1.6.1.jar 
wstx-asl-3.2.1.jar 
XmlSchema-1.2.jar 
xml-resolver-1.2.jar 

2.添加spring    jar包 

aopalliance-1.0.jar 
spring-core-2.0.4.jar 
spring-beans-2.0.4.jar 
spring-context-2.0.4.jar 
spring-web-2.0.4.jar 

3.构建webservice 本例子是基于jdk1.5 或者更高的版本 通过注解来构建webservice 

webservice的接口 

package demo.spring; 

import javax.jws.WebService; 

@WebService 
public interface HelloWorld { 
    String sayHi(String text); 


webservice 的接口实现 

package demo.spring; 

import javax.jws.WebService; 

@WebService(endpointInterface = "demo.spring.HelloWorld") 
public class HelloWorldImpl implements HelloWorld { 

    public String sayHi(String text) { 
        return "Hello " + text; 
    } 


4 。 spring bean的配置文件 

bean.xml 

<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" /> 

<bean id="hello" class="demo.spring.HelloWorldImpl" /> 

<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> 

<!-- 另一种写法 是 

<jaxws:endpoint    id="helloWorld"   implementor="demo.spring.HelloWorldImpl"     address="/HelloWorld" /> 

在这里我建议不要用这种方法 ,如果实现类有的属性要通过spring依赖注入的话,这种方法只是简单的new个实现类,他的属性没有通过spring依赖注入给注入值 

所有综合考虑   建议使用上面的写法! 

--> 
  
</beans> 

5. web.xml加载bean.xml文件 

<web-app> 
<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>WEB-INF/beans.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>/services/*</url-pattern> 
</servlet-mapping> 
</web-app> 

6.创建客户端配置文件 

client-bean.xml 

<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-2.0.xsd 
http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"> 

    <bean id="client" class="demo.spring.HelloWorld" 
      factory-bean="clientFactory" factory-method="create"/> 
    
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> 
   <property name="serviceClass" value="demo.spring.HelloWorld"/> 
   <property name="address" value="http://localhost:8080/HelloWorld"/> 
</bean> 
   
</beans> 

7.客户端访问 

ApplicationContext context = ...; // your Spring ApplicationContext 
HellWorld client = (HelloWorld) context.getBean("client"); 

OK 。。。其实就是这样的哦。。。。

你可能感兴趣的:(webservice)