在 Spring 配置文件中导入 CXF 提供 Schema、XML 详情

来源:互联网 发布:沈阳seo公司 编辑:程序博客网 时间:2024/06/06 17:07

本文转载:http://blog.csdn.net/cheng_feng_xiao_zhan/article/details/52959479


 3 . 在 Spring 配置文件中导入 CXF 提供 Schema、XML(cxf.jar 包里提供)
                * Schema 文件
                        <beans xmlns:jaxws="http://cxf.apache.org/jaxws" 
                                xsi:schemaLocation="
                                        http://cxf.apache.org/jaxws //命名空间
                                        http://cxf.apache.org/schemas/jaxws.xsd">//物理路径
                * 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"/>

 * 下面是一个简单的xml配置文件,我们来分析如何导入 CXF 提供 Schema、XML

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:p="http://www.springframework.org/schema/p"   
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"   
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  6.     xsi:schemaLocation="  
  7.         http://cxf.apache.org/jaxws  
  8.         http://cxf.apache.org/schemas/jaxws.xsd  
  9.         http://www.springframework.org/schema/beans   
  10.         http://www.springframework.org/schema/beans/spring-beans.xsd">  
  11.     <!-- Web应用的类加载路径有两类  
  12.     1.WEB-INF/classes目录  
  13.     2.WEB-INF/lib目录下  
  14.      -->  
  15.     <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  16.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
  17.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
  18.     <!-- 注册Service -->  
  19.     <bean id="userService" class="org.fjava.cxf.service.UserServiceImpl"/>  
  20.     <bean id="helloWorldWs" class="org.fjava.cxf.ws.impl.HelloWorldWs"  
  21.     p:userService-ref="userService"/>  
  22.     <!-- implementor指定WebService的服务提供者  
  23.     1.直接给定服务器提供者类名  
  24.     2.设置为容器中的Bean,要在Bean的id前加上#号  
  25.      -->  
  26.     <jaxws:endpoint implementor="#helloWorldWs" address="/getAllFoods">  
  27.         <!-- 添加了两个In拦截器 -->  
  28.         <jaxws:inInterceptors>  
  29.             <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/><!-- 嵌套Bean,创建一个Bean -->  
  30.             <bean class="org.fjava.cxf.ws.auth.AuthInterceptor"/>  
  31.             <!-- <ref bean="anotherInterceptor">引用一个已有的Bean -->  
  32.         </jaxws:inInterceptors>  
  33.         <!-- 需要配置Out拦截器,使用<jaxws:outInterceptors> -->  
  34.     </jaxws:endpoint>  
  35.     <!-- <jaxws:endpoint implementor="org.fjava.cxf.ws.impl.HelloWorldWs" address="/getAllFoods">  
  36.     </jaxws:endpoint> -->  
  37. </beans>  

 *  在 spring 配置文件中导入 CXF 提供 Schema 文件,在 cxf.jar 包里提供:

       

       

       导入效果:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:p="http://www.springframework.org/schema/p"   
  3.     xmlns:jaxws="http://cxf.apache.org/jaxws"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="  
  6.         http://cxf.apache.org/jaxws  
  7.         http://cxf.apache.org/schemas/jaxws.xsd  
  8.         http://www.springframework.org/schema/beans   
  9.         http://www.springframework.org/schema/beans/spring-beans.xsd">  

 * 导入 CXF 的 XML 文件:

       

       导入效果:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <import resource="classpath:META-INF/cxf/cxf.xml"/>  
  2. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>  
  3. <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>  
若导入 XML 文件错误,运行服务端报错:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. 十月 28, 2016 11:40:53 下午 org.apache.catalina.core.StandardContext listenerStart  
  2. 严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener  
  3. org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/cxf/cxff.xml]  
  4. Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/cxf/cxff.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxff.xml] cannot be opened because it does not exist  
  5.     at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)  
  6.     at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)  
  7.     at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)  
Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/cxf/cxff.xml]

修改正确 XML 的导入路径即可!

希望对你有帮助,祝你有一个好心情,加油!

若有错误、不全、可优化的点,欢迎纠正与补充;转载请注明出处!


0 0