您的位置:首页 > 其它

webservice之CXF注解实现(三)

2013-10-26 21:27 316 查看
如果项目中,想要用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"?>
<beans
xmlns="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.xsd http://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
@WebMethod
public String test(@WebParam RequestCondition requestCondition) {
// TODO Auto-generated method stub
return 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{

@Override
public String getDataFromDB() {
// TODO Auto-generated method stub
return "get data from db!";
}

}


使用上一篇webservice之CXF注解实现(二)中介绍的方式生成客户端代码进行测试

Invoking test...
test.result=get data from db!,年龄是:22
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: