maven整合CXF WebService+Spring @Resource无法注入问题解决方法

来源:互联网 发布:宿迁网络电视台 编辑:程序博客网 时间:2024/06/07 17:52

笔者是一个出道不长的小码农.工作中需要用的maven 结合jetty容器进行开发的前提背景下


废话不多说直接上代码


更改前的web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <display-name>cxf</display-name>    <!-- 配置spring框架 beans.xml -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:beans.xml</param-value>    </context-param>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:cxf-servlet.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>        <!-- 配置cxf框架 cxf-servlet.xml -->    <servlet>        <servlet-name>cxf</servlet-name>        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>        <init-param>            <param-name>config-location</param-name>            <param-value>classpath:cxf-servlet.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>cxf</servlet-name>        <url-pattern>/cxf/*</url-pattern>    </servlet-mapping></web-app>
更改前的beans.xml(spring配置文件)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/task                  http://www.springframework.org/schema/task/spring-task-3.0.xsd">         <context:annotation-config/>         <context:component-scan base-package="com"/></beans>
更改前的 cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><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="helloService" address="/hi" implementor="cn.HelloService">    <!-- 配置拦截器 -->    <jaxws:inInterceptors>    <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>    </jaxws:inInterceptors>    <jaxws:outInterceptors>    <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>    </jaxws:outInterceptors>    </jaxws:endpoint></beans>

相信很多刚接触webService cxf框架的小伙伴都是这么配置的.在页面访问也是成功的.小白我也是这样.单纯的以为能出页面就是大功告成了.

整合到项目中发现 @Resource 无法依赖注入.很是费解.因为已经 启用spring注解<context:annotation-config/>和扫描包<context:component-scan base-package="com"/>但是依旧是null

捞干的直接上改后代码+分析

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    <display-name>cxf</display-name>    <!-- 配置spring框架 beans.xml -->    <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:beans.xml</param-value>    </context-param>    <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:cxf-servlet.xml</param-value>    </context-param>    <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>        <!-- 配置cxf框架 cxf-servlet.xml -->    <servlet>    <servlet-name>cxf</servlet-name>    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    <init-param>    <param-name>config-location</param-name>    <param-value>classpath:beans.xml</param-value>    </init-param>    </servlet>    <servlet-mapping>    <servlet-name>cxf</servlet-name>    <url-pattern>/cxf/*</url-pattern>    </servlet-mapping></web-app>
beans.xml

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><context:annotation-config/><context:component-scan base-package="com"/>        <import resource="cxf-servlet.xml" /></beans>
cxf-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><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="helloService" address="/hi" implementor="cn.HelloService">    <!-- 配置拦截器 -->    <jaxws:inInterceptors>    <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>    </jaxws:inInterceptors>    <jaxws:outInterceptors>    <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>    </jaxws:outInterceptors>    </jaxws:endpoint></beans>

更改内容分别在web.xml与beans.xml,未进行更改cxf-servlet.xml

更改之前是web.xml-->beans.xml

                web.xml-->cxf-servlet.xml

更改之后是web.xml-->beans.xml-->cxf-servlet.xml

更改之前两个配置文件相互独立.启动时加载beans.xml访问加在cxf-servlet.xml.这个时候Spring的依赖注入并没有启动完全.且并没有报错.所以错误不是特别明显

更改一下配置文件的执行顺序就能有效解决Spring @Resource注解无法注入的问题了




1 0
原创粉丝点击