In diesem Leitfaden erfahren Sie, wie Sie Spring mit JAX-WS integrieren können, wie in diesem Link erwähnt: http://jax-ws-commons.java.net/spring/ . Wenn Sie dieses Lernprogramm abschließen, erstellen Sie einen einfachen HelloWorld-Webdienst (JAX-WS) und DI eine Bean über Spring in den Webdienst.
1. Projektordner
Siehe die endgültige Projektordnerstruktur.
2. Projektabhängigkeiten
Verwenden Sie Maven, um alle Abhängigkeiten der Bibliothek abzurufen. Der Schlüssel für die Integration von Spring mit JAX-WS ist jaxws-spring.jar .
Datei: pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4__0__0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong</groupId> <artifactId>WebServicesExample</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>WebServicesExample Maven Webapp</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>java.net</id> <url>http://download.java.net/maven/2</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <!-- JAX-WS --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2.3</version> </dependency> <!-- Library from java.net, integrate Spring with JAX-WS --> <dependency> <groupId>org.jvnet.jax-ws-commons.spring</groupId> <artifactId>jaxws-spring</artifactId> <version>1.8</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> <exclusion> <groupId>com.sun.xml.stream.buffer</groupId> <artifactId>streambuffer</artifactId> </exclusion> <exclusion> <groupId>org.jvnet.staxex</groupId> <artifactId>stax-ex</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <finalName>web services</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
3. JAX-WS Hallo Welt
Ein einfaches JAX-WS-Beispiel und Abhängigkeitsinjektion (DI) „HelloWorldBo“ über Spring.
Datei: HelloWorldWS.java
package com.mkyong.ws; import javax.jws.WebMethod; import javax.jws.WebService; import com.mkyong.bo.HelloWorldBo; @WebService public class HelloWorldWS{ //DI via Spring HelloWorldBo helloWorldBo; @WebMethod(exclude=true) public void setHelloWorldBo(HelloWorldBo helloWorldBo) { this.helloWorldBo = helloWorldBo; } @WebMethod(operationName="getHelloWorld") public String getHelloWorld() { return helloWorldBo.getHelloWorld(); } }
4. Bohnen
Hier ist die HelloWorldBo-Klasse mit einer
getHelloWorld ()
- Methode, um eine einfache Zeichenfolge zurückzugeben.
Datei: HelloWorldBo.java
package com.mkyong.bo; public interface HelloWorldBo{ String getHelloWorld(); }
Datei: HelloWorld Impl.java
package com.mkyong.bo.impl; import com.mkyong.bo.HelloWorldBo; public class HelloWorldBoImpl implements HelloWorldBo{ public String getHelloWorld(){ return "JAX-WS + Spring!"; } }
5. Konfiguration von Spring Beans
Spring-Beans-Konfigurationsdatei zum Binden des URL-Musters „ /hello “ an die Webserviceklasse „ com.mkyong.ws.HelloWorldWS “.
Datei: applicationContext.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:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd" > <wss:binding url="/hello"> <wss:service> <ws:service bean="#helloWs"/> </wss:service> </wss:binding> <!-- Web service methods --> <bean id="helloWs" class="com.mkyong.ws.HelloWorldWS"> <property name="helloWorldBo" ref="HelloWorldBo"/> </bean> <bean id="HelloWorldBo" class="com.mkyong.bo.impl.HelloWorldBoImpl"/> </beans>
-
Hinweis + Bei diesem Jaxws-Spring-Integrationsmechanismus ist die Datei sun-jaxws.xml ** nicht mehr erforderlich.
6. web.xml
In web.xml wird "com.sun.xml.ws.transport.http.servlet.WSSpringServlet" erklärt und mit "`/hello "verknüpft.
<web-app id="WebApp__ID" 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"> <display-name>Spring + JAX-WS</display-name> <servlet> <servlet-name>jaxws-servlet</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSSpringServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>jaxws-servlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <!-- Register Spring Listener --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
7. Demo
Starten Sie das Projekt und greifen Sie über die URL „ /hello “ auf den bereitgestellten Web-Service zu.
Laden Sie den Link herunter://wp-content/uploads/2011/03/JAX-WS-Spring-Integration-Example.zip[JAX-WS-Spring-Integration-Example.zip](10KB)
Referenz
-
link://webservices/jax-ws/jax-ws-java-web-anwendungsintegrationsbeispiel/[JAX-WS
+ Java-Webanwendungsintegrationsbeispiel]. http://jax-ws-commons.java.net/spring/