Spring整合CXF

CXF+Spring配置
1.服务端

接口定义:
@WebService
public interface UserService {
     String login();  
     User findById(User user);
     List<User> getUserList();
}

实现类:
@WebService
public class UserServiceImpl implements UserService {
    @Override
    public String login() {
    
        return "login success";
    }
    @Override
    public User findById(User user) {
    
        return user;
    }
    @Override
    public List<User> getUserList() {
    
        return list;
    }
}

发布CXF WebService服务:
applicationContext.xml配置文件

<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-3.1.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" />


    <!-- 1.使用jaxws:enpoint发布服务 -->
     <jaxws:endpoint address="/myService" implementor="com.demo.UserServiceImpl" />
    
    <!-- 2.使用jaxws:server发布服务 -->
    <bean id="UserServiceImpl" class="com.demo.UserServiceImpl" />
    <jaxws:server serviceClass="com.demo.UserService" address="/myService">
        <jaxws:serviceBean>
            <ref bean="UserServiceImpl" />
        </jaxws:serviceBean>
    </jaxws:server>
=============================================================================
2.客户端调用

相同接口:
public interface UserService {
     String login();  
     User findById(User user);
     List<User> getUserList();
}
调用方式1:
public static void test1() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(UserService.class);
        factory.setAddress("http://localhost:8080/CXFDemo/ws/myService");
        UserService userDao = (UserService) factory.create();
        User u = userDao.findById(new User(1, "aa", "234"));
        System.out.println(u);
}
    
调用方式2:
先要在applicationContext.xml中配置:

    <jaxws:client id="userService" serviceClass="com.demo.UserService"
        address="http://localhost:8080/CXFDemo/ws/myService"></jaxws:client>
        
public static void test2() {
    ApplicationContext ct = new ClassPathXmlApplicationContext(
                "classpath:applicationContext.xml");
        //UserService us = (UserService) ct.getBean("userService");
        UserService us = (UserService) ct.getBean(UserService.class);
        List<User> list = us.getUserList();
        System.out.print(list);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值