WebService CXF学习(入门篇4):整合Spring框架
通过前面两节的讲解,相信你对CXF框架开始有一些认识了。在当今项目开发中,Spring框架基上都用到过,那么它怎么与CXF结合呢,这就是我们这一间要讲的内容。好了,闲话少说。 <web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext-*.xml,/WEB-INF/classes/webservice.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> [color=red]<servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>[/color] </web-app>
@WebService public interface IService { //public void save(@WebParam(name="info")String xml); public void save(@WebParam(name="dto")UserInfoDTO dto,@WebParam(name="flag")boolean flag); public void update(@WebParam(name="info")String xml); public void delete(@WebParam(name="id")int id); public @WebResult(name="String")String get(@WebParam(name="id")int id); }
@WebService public class ServiceImpl implements IService { private Logger log = LoggerFactory.getLogger(ServiceImpl.class); public void delete(int id) { log.info("delete id is {} user"+id); } public void save(UserInfoDTO dto,boolean flag) { System.out.println("name:"+dto.getName()); } public void update(String xml) { } public String get(int id){ return null; } }
@XmlType(name="ServerUserInfo") @XmlAccessorType(XmlAccessType.FIELD) public class UserInfoDTO implements java.io.Serializable { private static final long serialVersionUID = -4666026219400887433L; private Integer id; private String name; private Integer age; private Integer address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getAddress() { return address; } public void setAddress(Integer address) { this.address = address; } public UserInfoDTO() { } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" [color=red]xmlns:jaxws="http://cxf.apache.org/jaxws"[/color] xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> [color=red] <!--导入与CXF框架有关的xml--> <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" />[/color] <!--布布WebService接口--> <jaxws:endpoint id="service" implementor="com.itdcl.service.ServiceImpl" address="/Service"> </jaxws:endpoint> </beans>
public class SampleClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "beans.xml"); IService service = (IService)context.getBean("service"); ServerUserInfo userInfo = new ServerUserInfo(); userInfo.setAddress(1); userInfo.setAge(100); userInfo.setName("Jason"); service.save(userInfo,true); service.delete(1); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" [color=red]xmlns:jaxws="http://cxf.apache.org/jaxws[/color]" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd [color=red]http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd[/color]"> <jaxws:client id="service" address="http://localhost:9999/cxf/Service" serviceClass="com.itdcl.service.IService" /> </beans> 留意一下绿色字体 ??? 剩下的事就是运行客户端程序了,没有异常抛出,打印出:name:Josen,则恭喜你CXF框架与Spring整合成功。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |