apache发布的cxf+spring案例
来源:互联网 发布:中国游戏产业 知乎 编辑:程序博客网 时间:2024/06/01 23:19
his example will lead you through creating your first service with Spring. You'll learn how to: This example corresponds to the spring_http example in the CXF distribution. Open up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory. The Spring jars: And the CXF jar: First we'll write our service interface. It will have one operation called "sayHello" which says "Hello" to whoever submits their name. Our implementation will then look like this: The @WebService annotation on the implementation class lets CXF know which interface to use when creating WSDL. In this case its simply our HelloWorld interface. CXF contains support for "nice XML" within Spring 2.0. For the JAX-WS side of things, we have a <jaxws:endpoint> bean which sets up a server side endpoint. Lets create a "beans.xml" file in our WEB-INF directory which declares an endpoint bean: If you want to reference a spring managed-bean, you can write like this: The bean uses the following properties: To provide a bean name instead of a classname as an implementor, simply supply the bean-name prepended with "#", e.g. implementor="#myBean". You can also do more sophisticated things with the <jaxws:endpoint> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the service. For more on this see JAX-WS Configuration. We'll need to add two things to our web.xml file: It is important to note that the address that you chose for your endpoint bean must be 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 a request. Just like the <jaxws:endpoint> used on the server side, there is a <jaxws:client> that can be used on the client side. You'll give it a bean name, the service interface, and the service URL, and it will create a bean with the specified name, implementing the service interface, and invoking the remote SOAP service under the covers: You can now inject that "helloClient" bean into any other Spring bean, or look it up from the Spring application context manually with code like this: You can also do more sophisticated things with the <jaxws:client> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the client. For more on this see JAX-WS Configuration. CXF includes a JaxWsProxyFactory bean which create a client for you from your service interface. You simply need to tell it what your service class is (the HelloWorld interface in this case) and the URL of your service. You can then create a client bean via the JaxWsProxyFactory bean by calling it's create() method. Here's an example: If you were going to access your client you could now simply pull it out of the Spring context (or better yet, inject it into your application using Spring!): client code athttp://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/demo/spring/client/Client.javaSetting up your build
commons-logging-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.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.jarjaxb-impl-2.0.5.jarjaxws-api-2.0.jarneethi-2.0.jarsaaj-api-1.3.jarsaaj-impl-1.3.jarstax-api-1.0.1.jarwsdl4j-1.6.1.jarwstx-asl-3.2.1.jarXmlSchema-1.2.jarxml-resolver-1.2.jar
aopalliance-1.0.jarspring-core-2.0.8.jarspring-beans-2.0.8.jarspring-context-2.0.8.jarspring-web-2.0.8.jar
cxf-2.1.jar
Writing your Service
package demo.spring;import javax.jws.WebService;@WebServicepublic interface HelloWorld { String sayHi(String text);}
package demo.spring;import javax.jws.WebService;@WebService(endpointInterface = "demo.spring.HelloWorld")public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { System.out.println("sayHi called"); return "Hello " + text; }}
Declaring your server 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.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-extension-soap.xml" /><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="helloWorld" implementor="demo.spring.HelloWorldImpl" address="/HelloWorld" /> </beans>
<bean id="hello" class="demo.spring.HelloWorldImpl" /><jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
Setting up the Servlet
<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>/*</url-pattern></servlet-mapping></web-app>
Create a Client (Easy Way)
<?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" serviceClass="demo.spring.HelloWorld" address="http://localhost:9002/HelloWorld" /></beans>
ApplicationContext context = ...; // your Spring ApplicationContextHellWorld client = (HelloWorld) context.getBean("helloClient");
Create a Client (More Manual Way)
<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" 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:9002/HelloWorld"/></bean> </beans>
ApplicationContext context = ...; // your Spring ApplicationContextHellWorld client = (HelloWorld) context.getBean("client");
- apache发布的cxf+spring案例
- Spring 整合 Apache CXF发布webService
- CXF Spring整合案例
- spring中发布基于cxf的webservice
- webservice 基于spring的cxf发布
- cxf+spring发布webservice
- cxf+spring发布webservice
- spring +cxf 发布webservice
- spring+cxf发布webservice
- CXF+Spring发布webservice
- apache cxf ws spring
- Spring整合Apache CXF
- Spring+Apache CXF集成
- spring集成cxf简单案例
- spring整合cxf入门案例
- spring+cxf发布服务报错Error creating bean with name 'org.apache.cxf.jaxws.spring.NamespaceHandler$SpringSe
- Web应用使用Apache CXF 建立 WebService——详细案例(与Spring结合)
- cxf+spring完成webservice发布
- SDK Platform Tools component is missing! Please ×××× Install×××解决方法
- Android 内核源代码交叉编译(创建自己的模拟器内核,哈哈终于搞定了)
- 通过拼sql语句实现分页读取数据
- Struts2.2.1配置时需要多加3个JAR包
- Gabor滤波参数详细解析
- apache发布的cxf+spring案例
- 【转】强大的poj分类
- IE掉FireFox
- 更新
- C#创建文件夹并设置权限
- C语言变量声明内存分配
- ACM训练计划表
- 特殊的颜色 [UIColor clearColor]
- 八皇后夹墙版