JAX-WS整合Spring
提示:service类中@Autowired失效:web.xml中的Servlet,需要用spring的servlet替换,即com.sun.xml.ws.transport.http.servlet.WSSpringServlet不是com.sun.xml.ws.transport.http.servlet.WSServlet
JAX-WS整合Spring
1.jax-ws需要的jar包:
activation.jar,
FastInfoset.jar,
gmbal-api-only.jar,
ha-api.jar,
http.jar,
jaxb-api.jar,
jaxb-impl.jar,
jaxb-xjc.jar,
jaxws-api.jar,
jaxws-rt.jar,
jaxws-tools.jar,
jsr173_api.jar,
jsr181-api.jar,
jsr250.jar,
management-api.jar,
mimepull.jar,
policy.jar,
resolver.jar,
saaj-api.jar,
saaj-impl.jar,
stax-ex.jar,
streambuffer.jar,
woodstox.jar
还有集成需要的jar包jaxws-spring-1.8.jar,xbean-spring-3.7.jar
2.web.xml中对ContextLoaderListener的声明一定要在WSServletContextListener的前面;WS服务类要继承SpringBeanAutowiringSupport后才能使用@Autowired。
3.web.xml中的Servlet,对,就是Servlet,是com.sun.xml.ws.transport.http.servlet.WSSpringServlet不是com.sun.xml.ws.transport.http.servlet.WSServlet,需要用spring的servlet替换
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 注意:文件名通配符在Weblogic中无效,会导致SpringContext初始化失败,这里请枚举每个配置文件。(路径通配符似乎可以) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring.xml
</param-value>
</context-param>
<!-- Spring Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Hello</servlet-name>
<!--
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
-->
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloService</url-pattern>
</servlet-mapping>
</web-app>
4.配置文件jax-ws的sun-jaxws.xml:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0"
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
<endpoint implementation="com.pand.food.ws.HelloService" name="Hello"
url-pattern="/HelloService" />
</endpoints>
spring的spring-ws.xml:
<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.0.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="/HelloService">
<wss:service>
<ws:service bean="#helloService" />
</wss:service>
</wss:binding>
<bean id="helloService" class="com.pand.food.ws.HelloService" />
</beans>
spring的spring-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="loginfoService" class="com.pand.food.service.LoginfoService"/>
</beans>
spring的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:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.pand.food" />
<import resource="classpath:spring/spring*.xml" />
</beans>
5.service类:
LoginfoService.java:
package com.pand.food.service;
public class LoginfoService {
private String log;
public String getLog() {
return log;
}
public void setLog(String log) {
this.log = log;
}
}
Constants.java:
package com.pand.food.ws;
public class Constants {
public final static String WS_NAMESPACE = "http://www.pand.com/";
}
Hello.java:
package com.pand.food.ws;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.WebResult;
@WebService(name = "HelloService", targetNamespace = Constants.WS_NAMESPACE)
public interface Hello {
@WebResult(name = "result")
public String sayHello(@WebParam(name = "name") String name);
}
HelloService.java:
package com.pand.food.ws;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.pand.food.service.LoginfoService;
@WebService(endpointInterface = "com.pand.food.ws.Hello", serviceName = "HelloService", targetNamespace = Constants.WS_NAMESPACE)
public class HelloService extends SpringBeanAutowiringSupport implements Hello {
@Autowired
private LoginfoService loginfoService;
public String sayHello(String name) {
System.out.println("hello " + name);
if(loginfoService == null){
System.out.println("Autowired failed");
}else {
System.out.println("Autowired successfully");
loginfoService.setLog(name);
return "hello " + loginfoService.getLog();
}
return "hello " + name;
}
}
JAX-WS整合Spring
1.jax-ws需要的jar包:
activation.jar,
FastInfoset.jar,
gmbal-api-only.jar,
ha-api.jar,
http.jar,
jaxb-api.jar,
jaxb-impl.jar,
jaxb-xjc.jar,
jaxws-api.jar,
jaxws-rt.jar,
jaxws-tools.jar,
jsr173_api.jar,
jsr181-api.jar,
jsr250.jar,
management-api.jar,
mimepull.jar,
policy.jar,
resolver.jar,
saaj-api.jar,
saaj-impl.jar,
stax-ex.jar,
streambuffer.jar,
woodstox.jar
还有集成需要的jar包jaxws-spring-1.8.jar,xbean-spring-3.7.jar
2.web.xml中对ContextLoaderListener的声明一定要在WSServletContextListener的前面;WS服务类要继承SpringBeanAutowiringSupport后才能使用@Autowired。
3.web.xml中的Servlet,对,就是Servlet,是com.sun.xml.ws.transport.http.servlet.WSSpringServlet不是com.sun.xml.ws.transport.http.servlet.WSServlet,需要用spring的servlet替换
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 注意:文件名通配符在Weblogic中无效,会导致SpringContext初始化失败,这里请枚举每个配置文件。(路径通配符似乎可以) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring.xml
</param-value>
</context-param>
<!-- Spring Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Hello</servlet-name>
<!--
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
-->
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloService</url-pattern>
</servlet-mapping>
</web-app>
4.配置文件jax-ws的sun-jaxws.xml:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints version="2.0"
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime">
<endpoint implementation="com.pand.food.ws.HelloService" name="Hello"
url-pattern="/HelloService" />
</endpoints>
spring的spring-ws.xml:
<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.0.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="/HelloService">
<wss:service>
<ws:service bean="#helloService" />
</wss:service>
</wss:binding>
<bean id="helloService" class="com.pand.food.ws.HelloService" />
</beans>
spring的spring-service.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="loginfoService" class="com.pand.food.service.LoginfoService"/>
</beans>
spring的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:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.pand.food" />
<import resource="classpath:spring/spring*.xml" />
</beans>
5.service类:
LoginfoService.java:
package com.pand.food.service;
public class LoginfoService {
private String log;
public String getLog() {
return log;
}
public void setLog(String log) {
this.log = log;
}
}
Constants.java:
package com.pand.food.ws;
public class Constants {
public final static String WS_NAMESPACE = "http://www.pand.com/";
}
Hello.java:
package com.pand.food.ws;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.WebResult;
@WebService(name = "HelloService", targetNamespace = Constants.WS_NAMESPACE)
public interface Hello {
@WebResult(name = "result")
public String sayHello(@WebParam(name = "name") String name);
}
HelloService.java:
package com.pand.food.ws;
import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.pand.food.service.LoginfoService;
@WebService(endpointInterface = "com.pand.food.ws.Hello", serviceName = "HelloService", targetNamespace = Constants.WS_NAMESPACE)
public class HelloService extends SpringBeanAutowiringSupport implements Hello {
@Autowired
private LoginfoService loginfoService;
public String sayHello(String name) {
System.out.println("hello " + name);
if(loginfoService == null){
System.out.println("Autowired failed");
}else {
System.out.println("Autowired successfully");
loginfoService.setLog(name);
return "hello " + loginfoService.getLog();
}
return "hello " + name;
}
}