webservice之CXF注解实现(三)
来源:互联网 发布:应用包安装软件 编辑:程序博客网 时间:2024/06/05 10:39
如果项目中,想要用CXF提供webService服务,又想使用Spring做依赖注入和AOP,Spring能将CXF整合么,答案是肯定的。
看下面的web.xml配置
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>cxfTest</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- cfx webSerivice --> <servlet> <servlet-name>cxf</servlet-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config> </web-app>
是的,这个配置没有什么特别的,在原有的cxf配置基础上,加入了spring的配置,但是注意<context-param>的配置,使得cxf-servlet.xml不是必须的文件了,也就是我们可以在applicationContext.xml中进行CXF的配置。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><!-- 配置Service实现类以及dao实现类 --><bean id="IServiceImpl" class="com.look.service.IServiceImpl"> <property name="idao" ref="idaoImpl"></property></bean><bean id="idaoImpl" class="com.look.dao.IDaoImpl" /><!-- cxf webservice 服务 配置 begin --> <!--serviceClass定义了webService服务的入口,address定义了请求的名称 --><jaxws:server id="IService" serviceClass="com.look.service.IService" address="/IService"> <jaxws:serviceBean> <ref bean="IServiceImpl"/><!-- 引用IServiceImpl实现 IService接口--> </jaxws:serviceBean> </jaxws:server> </beans>这里使用了依赖注入注入了idao,另外配置了访问路径为IService的webService接口
有些文章中还引用了下面的配置
<!-- 引入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"/>
在我的配置中,我没有引入他们,也没有任何的报错信息,这里我就不引入了,有兴趣的朋友可以自己研究一下
下面贴出相关bean的代码
ISerivceImpl.java
package com.look.service;import javax.jws.WebMethod;import javax.jws.WebParam;import com.look.dao.IDao;import com.look.model.RequestCondition;public class IServiceImpl implements IService{private IDao idao;public IDao getIdao() {return idao;}public void setIdao(IDao idao) {this.idao = idao;}@Override@WebMethodpublic String test(@WebParam RequestCondition requestCondition) {// TODO Auto-generated method stubreturn idao.getDataFromDB()+",年龄是:"+requestCondition.getAge();}}
IDao.java
package com.look.dao;public interface IDao {String getDataFromDB();}IDaoImpl.java
package com.look.dao;public class IDaoImpl implements IDao{@Overridepublic String getDataFromDB() {// TODO Auto-generated method stubreturn "get data from db!";}}
使用上一篇webservice之CXF注解实现(二)中介绍的方式生成客户端代码进行测试
Invoking test...test.result=get data from db!,年龄是:22
- webservice之CXF注解实现(三)
- webservice之CXF注解实现(一)
- webservice之CXF注解实现(二)
- WebService之CXF注解之三(Service接口实现类)
- WebService之CXF注解报错(三)
- cxf webservice 注解方式实现!!!!!
- webService之(三)CXF服务端
- webService实现之cxf
- WebService之CXF注解之二(Service接口)
- WebService之CXF注解之四(测试类)
- WebService之CXF注解之五(配置文件)
- WebService之CXF+Spring集成(使用注解)
- WebService之CXF注解报错(一)
- WebService之CXF注解报错(二)
- WebService之CXF注解之一(封装类)
- (三)WebService之CXF发布Web服务
- WebService-CXF和spring整合实现webservice案例(Spring配置注解环境)
- CXF实现webservice (一)
- 2014年10月26完美世界校招java中的两道java题
- Login failed for user 'IIS APPPOOL\DefaultAppPool'
- Spring MVC中redirect与forward
- 安装MangOS
- ER模型转关系模型
- webservice之CXF注解实现(三)
- Android 读写xml文件
- C++_拷贝构造函数
- cocos2d-x ccsprite 详解
- AutoCAD常用快捷键
- java中==和equals的区别
- 基于struts2框架下的验证码实现
- C++_指针悬挂和赋值操作符的重载
- 第一篇