1、a > 建一個web項目,添加完spring+hibernate的jar包以後,添加jax-ws 2.1等jar路徑;
b > 然後寫一個java類,帶上@WebService和@WebMethod,
c > 使用Myeclipse自動生成WS,會自動生成wsdl、web.xml中的配置等,applicationContext.xml中不用額外配置;
(加上@,則不會生成TgsFuncitonDelegate)
d > 在spring中,toTell中注入private WSAction wsaction,wsaction使用時報空指針,執行個體化toTell時wsaction沒有注入成功,需要這樣得到wsaction
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
WSAction wsaction= (TgsPassedCarAction)context.getBean("WSAction");
e > (jar包:spring +
hibernate +
jax-ws 2.1 RunTime Liberaries +
jax-ws 2.1 API Liberaries)
----------------------------------------------------------web.xml中自動生成的部分---------------------------------------------------------------------------------------------------------
-------------------------------------還有記得是WSServlet,不要和後邊的WSSpringServlet混了----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<span style="color:#ff0000;"> <listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener
</listener-class>
</listener>
</span> <servlet>
<description>JAX-WS endpoint - toDoService</description>
<display-name>toDoService</display-name>
<servlet-name>toDoService</servlet-name>
<servlet-class>
<span style="color:#ff0000;">com.sun.xml.ws.transport.http.servlet.WSServlet</span>
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>toDoService</servlet-name>
<url-pattern>/toDoPort</url-pattern>
</servlet-mapping>
</web-app>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2、通過spring注入的方式,而不用再context.getBean()的方式獲得注入的bean元件,更加友善
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
a > 首先加上Spring+jax-ws整合所需要的jar包,見下面,spring和hibernate的jar包随意加;
b > 寫一個java類,要加上@WebService(targetNamespace = "org.coderecord.blog"),括号裡的沒加的話,我這裡報錯(Caused by: com.sun.xml.ws.model.RuntimeModelerException: 必須在沒有程式包的類上指定 @WebService.targetNamespace。類: Test);
@WebMethod(exclude = true)使get/set方法對外不可見;
<p> import javax.jws.WebMethod;
import javax.jws.WebService;</p><p> @WebService(targetNamespace = "org.coderecord.blog")
public class Test {
private TestBean testBean;
@WebMethod
public String toTest(String name){
String daming = testBean.tosat("lvlvlv");
System.out.println(name+": "+daming);
return name+":"+daming;
}
@WebMethod(exclude = true)
public TestBean getTestBean() {
return testBean;
}
@WebMethod(exclude = true)
public void setTestBean(TestBean testBean) {
this.testBean = testBean;
}
}</p>
c > 接着,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" <span style="color:#ff0000;">xmlns:ws="http://jax-ws.dev.java.net/spring/core"
</span> <span style="color:#ff0000;"> xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"</span> xmlns:p="http://www.springframework.org/schema/p"
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/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
<span style="color:#ff0000;"> 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"></span>
<bean id="test" class="Test">
<property name="testBean">
<ref bean="testBean"/>
</property>
</bean>
<bean id="testBean" class="TestBean"></bean>
<span style="color:#ff0000;"> <wss:binding url="/service/test">
<wss:service>
<ws:service bean="#test" />
</wss:service>
</wss:binding>
</span></beans>
d > web.xml中手動配置,注意下面紅色的代碼和第一種jax-ws寫法中的WSServlet 不是一個Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/*Context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<description>JAX-WS endpoint - test</description>
<display-name>testService</display-name>
<servlet-name>testService</servlet-name>
<span style="color:#ff0000;"><servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
</span> <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testService</servlet-name>
<url-pattern>/service/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>