---
title: "cxf 与spring集成"
author: "jiang2008"
date: "2014-11-14T10:47:23Z"
tags: []
categories: ["编程语言"]
slug: "cxf-spring"
draft: false
lastmod: "2014-11-14T10:53:28Z"
id: 2155908
url: "https://jiang2008.iteye.com/blog/2155908"
---

一：在web.xml中增加CXFServlet的配置：

 

```
<servlet>
	<description>Apache CXF Endpoint</description>
	<display-name>cxf</display-name>
	<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>
```

 二：新建applicationContext-cxf.xml，配置endpoint ，url等信息

```
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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.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-servlet.xml"/>
    <jaxws:endpoint id="idssWervices" implementor="com.easylife.function.sample2.server.DssWebServicesImpl" address="/idssWervices"/>
    <jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>
</beans>
<!-- END SNIPPET: beans -->
```

  根据spring contextConfigLocation加载规则，我们把applicationContext-cxf.xml放到classpath:config/context/下。

```
<context-param> 
 <param-name>contextConfigLocation</param-name> 
 <param-value>classpath:config/context/applicationContext*.xml</param-value> 
</context-param>
```

 

三：测试

```
@WebService(endpointInterface = "com.easylife.function.sample2.webInterface.IdssWervices")
public class DssWebServicesImpl implements IdssWervices {
	  Map<Integer, User> users = new LinkedHashMap<Integer, User>();
	  @Autowired
	  ChangeOutInServive changeOutInServive;
      public String sayHi(String text) {
                  return "Hello " + text;
     }

     public String sayHiToUser(User user) {
    	 changeOutInServive.getBomPartCode("");
               users.put(users.size()+1, user);
               return "Hello "+ user.getName();
     }
```

 

```
@RequestMapping("/helloworld")
public String test(Model model){
	JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
        svr.setServiceClass(IdssWervices.class);
        svr.setAddress("http://localhost:8082/easylife/services/idssWervices");
        IdssWervices hw = (IdssWervices) svr.create();
        User user = new User();
        user.setName("textYYh");
        user.setDescription("test");
        String response =  hw.sayHiToUser(user);
        model.addAttribute("msg",response );
        return "webservicetest";
}
```

 经测试changeOutInServive 已经注入，和spring集成成功。