Cxf 2.5.2 + spring 开发配置
最近在使用cxf时发现,本文主要参考cxf使用手册
http://cxf.apache.org/docs/writing-a-service-with-spring.html
?
?
第一步:设置你的cxf的classpath
1、依赖库
?
commons-logging-1.1.1.jargeronimo-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.7.1.jar (or Sun's JavaMail jar)geronimo-servlet_3.0_spec-1.0.jar (or Sun's Servlet jar)geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)jaxb-api-2.1.jarjaxb-impl-2.1.13.jarjaxws-api-2.1.jarneethi-3.0.0.jarsaaj-api-1.3.jarsaaj-impl-1.3.jarstax-api-1.0.1.jarstax2-api-3.1.1.jarwsdl4j-1.6.2.jarwoodstox-core-asl-4.0.8.jarxmlschema-core-2.0.jarxml-resolver-1.2.jar?
2、The Spring jars:
?
aopalliance-1.0.jarspring-core-3.0.5.RELEASE.jarspring-beans-3.0.5.RELEASE.jarspring-context-3.0.5.RELEASE.jarspring-web-3.0.5.RELEASE.jar?
3、And the CXF jar:
?
cxf-2.4.0.jar?
第二步 编写你的service
1、代码编写
?
接口:package demo.spring.service;import javax.jws.WebService;@WebServicepublic interface HelloWorld { String sayHi(String text);}接口实现:package demo.spring.service;import javax.jws.WebService;@WebService(endpointInterface = "demo.spring.service.HelloWorld")public class HelloWorldImpl implements HelloWorld {public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; }}?2、spring文件配置
Lets create a "cxf-servlet.xml" file in our WEB-INFdirectory which declares an endpoint bean。cxf-servlet.xml默认在WEB-INF directory但是可以在web.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.xsdhttp://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-servlet.xml" /><jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld" /> </beans>另外的配置方式If you want to reference a spring managed-bean, you can write like this:<bean id="hello" /><jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />?
?
The beanuses the following properties:
To provide abean name instead of a classname as an implementor, simply supply the bean-nameprepended with "#", e.g. implementor="#myBean".
Youcan also do more sophisticated things withthe?<jaxws:endpoint>?element like add nested tags to attachJAX-WS Handlers or CXF Interceptors to the service. For more on this see?JAX-WSConfiguration.
第三步设置cxf的servlet
Sincewe're relying on the default "cxf-servlet.xml" file the?defaultweb.xml?referenced by many samples can beused.
Alternatively,for arbitrarily named configuration files such as beans.xml,application-context.xml, etc. we can add the following elements:
An example:
?
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.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>/*</url-pattern></servlet-mapping>?
It isimportant to note that the address that you chose for your endpoint bean mustbe one your servlet listens on. For instance, if my Servlet was register for"/some-services/*" but my address was"/more-services/HelloWorld", there is no way CXF could receive arequest.
Create a Client (Easy Way)
Just likethe?<jaxws:endpoint>?used on the server side, there isa?<jaxws:client>?that can be used on the client side. You'llgive it a bean name, the service interface, and the service URL, and it willcreate a bean with the specified name, implementing the service interface, andinvoking the remote SOAP service under the covers:
?
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><jaxws:client id="helloClient" service/></beans>?
You can nowinject that "helloClient" bean into any other Spring bean, or look itup from the Spring application context manually with code like this:
ApplicationContext context = ...; // your Spring ApplicationContextHellWorld client = (HelloWorld) context.getBean("helloClient");?Youcan also do more sophisticated things withthe?<jaxws:client>?element like add nested tags to attachJAX-WS Handlers or CXF Interceptors to the client. For more on this see?JAX-WSConfiguration.
Create a Client (More Manual Way)
CXF includesa JaxWsProxyFactory bean which create a client for you from your serviceinterface. You simply need to tell it what your service class is (theHelloWorld interface in this case) and the URL of your service. You can thencreate a client bean via the JaxWsProxyFactory bean by calling it's create()method.
Here's anexample:
?
<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.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd"><bean id="client" factory-bean="clientFactory" factory-method="create"/> <bean id="clientFactory" value="demo.spring.service.HelloWorld"/> <property name="address" value="http://localhost:9002/services/HelloWorld"/> </bean> </beans>?
If you weregoing to access your client you could now simply pull it out of the Springcontext (or better yet, inject it into your application using Spring!):
?
ApplicationContext context = ...; // your Spring ApplicationContextHelloWorld client = (HelloWorld) context.getBean("client");?clientcode at?http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/client/Client.java
?
?