一、新建web工程:
略
二、引入相应jar包:
因只为了简单测试,此处未增加过多的jar包(但经过自己测试,已完全满足简单的功能),jar包如下(包含Spring所需要的jar包):
wss4j-1.6.4.jar
log4j-1.2.16.jar
spring-aop-3.0.6.RELEASE.jar
spring-asm-3.0.6.RELEASE.jar
spring-beans-3.0.6.RELEASE.jar
spring-context-3.0.6.RELEASE.jar
spring-core-3.0.6.RELEASE.jar
spring-expression-3.0.6.RELEASE.jar
commons-logging-1.1.1.jar
spring-web-3.0.6.RELEASE.jar
cxf-2.5.2.jar
neethi-3.0.1.jar
xmlschema-core-2.0.1.jar
wsdl4j-1.6.2.jar
xmlsec-1.4.6.jar
xalan-2.7.1.jar
serializer-2.7.1.jar
三、配置web.xml:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/beans.xml,/WEB-INF/classes/webservices.xml</param-value> </context-param> <!-- Spring ContextLoaderListener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Apache CXFServlet --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- CXFServlet Mapping --> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
说明:该配置中另加载了两个文件beans.xml、webservices.xml,这两个文件主要是用来配置service与发布接口用的,在下面的配置文件中将进行介绍。
四、编写应该程序、相应配置文件:
编写一个简单的测试方法,使用Spring对其进行配置,代码如下beans.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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <!-- Service --> <bean id="testService" class="test.service.impl.TestServiceImpl" /> </beans>
当测试代码编写完成后,则将其接口发布,使其方便外部程序调用,其发布代码如下webservices.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Import Apache CXF Bean Definition -->
<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:server id="testWebService" serviceClass="test.service.ITestService"
address="/TestService">
<jaxws:serviceBean>
<ref bean="testService" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
注:在编写接口跟其实现时,需要在相应接口、类前面加上“@WebService”标注,否则不能正确生成客户端代码,如:
@WebService
public interface ITestService {
......
}
@WebService
public class TestServiceImpl implements ITestService {
......
}
五、部署工程、测试:
当接口发布完成后,一个简单的WevService服务端就编写完成了,接下来就进行一个简单的测试:
在浏览器中访问定义的接口。如:http://localhost:8080/TestService?wsdl,结果如下图: