Server:
//1.创建发布服务的工厂
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
//2.设置服务地址
factoryBean.setAddress("http://localhost:8888/book");
//3.设置服务接口,发布的服务
factoryBean.setServiceClass(BookService.class);
//4。设置服务实现
factoryBean.setServiceBean(new BookServiceImpl());
//5.创建服务并启动
Server server=factoryBean.create();
server.start();
Client:
//1.代理服务工厂
JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();
//2.设置port
factoryBean.setAddress("http://localhost:8888/book?wsdl");
//3.服务实现
factoryBean.setServiceClass(BookService.class);
//4.创建客户端
BookService service=(BookService)factoryBean.create();
Cxf和SpringMVC整合
server端:
web.xml的配置:
<!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>
<!--编码过滤器-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--CXFServlet-->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:spring-cxf.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<!---->
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
Server端的springmvc配置:
<?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:cxf="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<context:component-scan base-package="com.etoak"></context:component-scan>
<!--发布服务
address:服务地址
serviceClass:
serviceBean:
-->
<cxf:server address="/book" serviceClass="com.etoak.service.BookService">
<cxf:serviceBean>
<ref bean="impl"></ref>
</cxf:serviceBean>
</cxf:server>
</beans>
Client端的配置:
<?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:cxf="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<cxf:client address="http://localhost:8080/ws/book"
serviceClass="com.etoak.service.BookService"
id="clientbookService"
>
</cxf:client>
</beans>