您的位置:首页 > 编程语言 > Java开发

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

2016-10-28 23:51 609 查看
 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 的配置文件
[b]                        
<import resource="classpath:META-INF/cxf/cxf.xml"/>
[b]                        
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
                        <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>[/b]
[/b]

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Web应用的类加载路径有两类
1.WEB-INF/classes目录
2.WEB-INF/lib目录下
-->
<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"/>
<!-- 注册Service -->
<bean id="userService" class="org.fjava.cxf.service.UserServiceImpl"/>
<bean id="helloWorldWs" class="org.fjava.cxf.ws.impl.HelloWorldWs"
p:userService-ref="userService"/>
<!-- implementor指定WebService的服务提供者
1.直接给定服务器提供者类名
2.设置为容器中的Bean,要在Bean的id前加上#号
-->
<jaxws:endpoint implementor="#helloWorldWs" address="/getAllFoods">
<!-- 添加了两个In拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/><!-- 嵌套Bean,创建一个Bean -->
<bean class="org.fjava.cxf.ws.auth.AuthInterceptor"/>
<!-- <ref bean="anotherInterceptor">引用一个已有的Bean -->
</jaxws:inInterceptors>
<!-- 需要配置Out拦截器,使用<jaxws:outInterceptors> -->
</jaxws:endpoint>
<!-- <jaxws:endpoint implementor="org.fjava.cxf.ws.impl.HelloWorldWs" address="/getAllFoods">
</jaxws:endpoint> -->
</beans>


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

       


       


       导入效果:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


 * 导入 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"/>
若导入 XML 文件错误,运行服务端报错:

十月 28, 2016 11:40:53 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/cxf/cxff.xml]
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
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
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 的导入路径即可!

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

若有错误、不全、可优化的点,欢迎纠正与补充;转载请注明出处!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cxf schema spring XML
相关文章推荐