Spring 整合CXF
搭建工程spring+cxf工程。添加相应的.jar
Interface:
?
import javax.jws.WebService;
?
@WebService
public interface UserService {
?
public String getUserName(String name);
?
public String getPassWord(String password);
}
Impl:
?
import javax.jws.WebService;
?
@WebService(endpointInterface = "com.bsf.gwtservice.UserService")
public class UserServiceImpl implements UserService {
@Override
public String getPassWord(String password) {
return MD5.getMDFivePassword(password);
}
?
@Override
public String getUserName(String name) {
return name;
}
}
spring-cxf.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://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" />
?
?
<bean id="userServiceImplCxf"
/>
?
<jaxws:endpoint id="userService" implementor="#userServiceImplCxf"
address="/userServiceCxf" />
</beans>
?
注意:<jaxws:endpoint id="userService" implementor="#userServiceImplCxf"
address="/userServiceCxf" />
id:指在spring配置的bean的ID.
Implementor:#+实现类的bean id.
implementorstyle="font-size: 14px; text-align: left; background-color: white; margin-top: 12pt; margin-right: 0cm; margin-bottom: 6pt; margin-left: 0cm; text-indent: 24pt; line-height: 14.95pt;">Address:指明这个web service的相对地址
配置web.xml
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:xml/*.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
部署到tomcat服务器,输入:http://localhost:9100/GwtService/userServiceCxf?wsdl
客户端:
用DOS将目录切换到cxf/bin运行wsdl2java.bat生成客户端代码
输入:wsdl2java -d src - client?http://localhost:9000/helloWorld?wsdl其作用上面的build.xml作用一样。附加:wsdl2java用法:wsdl2java -p com -d src -all? aa.wsdl-p? 指定其wsdl的命名空间,也就是要生成代码的包名:-d? 指定要产生代码所在目录-client 生成客户端测试web service的代码-server 生成服务器启动web? service的代码-impl 生成web service的实现代码-ant? 生成build.xml文件-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementationobject, and an Ant build.xml file.?
配置client-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:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:customer="http://customerservice.example.com/"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
">
<jaxws:client id="userService"
address="http://localhost:9100/GwtService/userServiceCxf"
servicestyle="background-color: white; margin-top: 12pt; margin-right: 0cm; margin-bottom: 6pt; margin-left: 0cm; text-indent: 24pt;"></jaxws:client>
</beans>
测试类:public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("test/client-applicationContext.xml");UserService service = (UserService) context.getBean("gpsService");System.out.println(service.getPassWord("password"));}?
?
?