本文是SOAP系列的结尾篇,贴近实战,讲解如何使用Spring集成ApacheCXF进行SOAP的发布。
将CXF交给Spring进行管理,更加便于业务逻辑的开发。
首先建立一个基于Servlet2.5的web工程,将spring以及cxf的包导入。
可以通过init-param制定cxf-servlet.xml配置文件的位置,本文采用第二种方式,将cxf的配置文件与spring配置文件一同加载。
通过查看cxf源码,可以看到默认查找/WEB-INF/cxf-servlet.xml,本文还是显式制定了文件位置。
<!-- 添加cxf支持 -->
<servlet>
<display-name>cxf</display-name>
<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:cxf-servlet.xml</param-value>
</init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- 初始化spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:cxf-servlet.xml,classpath*:beans.xml</param-value>
</context-param>
通过加载上下文加载监听器加载spring容器
这里要注意beans.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<context:component-scan base-package="com.person"/>
</beans>
在配置文件中制定了注解扫描包位置为com.person
编写DAO public interface PersonDao { public void add(Person person);
public List<Person> getAll();
}
@Repository(value="personDaoImpl")
public class PersonDaoImpl implements PersonDao {
List<Person> persons = new ArrayList<Person>();
public void add(Person person) {
persons.add(person);
}
public List<Person> getAll() {
return persons;
}
}
编写Service @WebService @Service @BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING) public interface PersonServiceWS {
public void add(Person person);
public List<Person> getAll();
}
将webservice注解加在接口上,制定SOAP协议版本为1.2
@Service
public class PersonServiceWSImpl implements PersonServiceWS {
@Autowired
PersonDao personDao;
public void add(Person person) {
personDao.add(person);
}
public List<Person> getAll() {
return personDao.getAll();
}
}
服务实现类继承接口,通过自动组装注入了Dao并执行其方法
<?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<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" />
<!-- 接口方式发布
serviceClass 接口
-->
<jaxws:server address="/person" serviceClass="com.person.person.ws.service.PersonServiceWS">
<!-- 实现类 -->
<jaxws:serviceBean>
<bean class="com.person.person.ws.serviceImpl.PersonServiceWSImpl"></bean>
</jaxws:serviceBean>
<!-- 拦截器配置输入拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<!-- 输出拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:server>
</beans>
访问http://localhost:8080/Spring_CXF/service/person?wsdl查看web服务描述语言
本文中的业务为测试业务,真实的业务中应当通过认证的方式对服务接口进行保护,比如通过身份密码认证的方式对访问者进行过滤,达到相对安全的调用目的。