首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

解決 XFire 發布 WebService 時 Spring 注入失效問題

     編輯:Java 相關知識

問題現象

使用 XFire 發布 WebService,Service 實現類中 @Autowired 注入的 Mapper 始終為 null,導致空指針異常。常規 Spring 掃描、@Component 等手段均無效。

根本原因

XFire 預設使用 XFireConfigurableServlet 且通過 services.xml 配置服務,此模式下服務實例由 XFire 容器管理,不在 Spring 容器生命週期內,故無法享受 Spring 依賴注入。

正確整合方案:XFireSpringServlet + Spring 管理 Bean

1. web.xml 關鍵修改

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
</context-param>

<servlet>
    <servlet-name>XFireServlet</servlet-name>
    <!-- 關鍵:使用 Spring 整合 Servlet -->
    <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>XFireServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

2. xfire-servlet.xml 將服務導出為 Spring Bean

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <!-- 引入 XFire 核心配置 -->
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

    <!-- URL 映射到服務 Bean -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/SSOMethodsServices"><ref bean="sso"/></entry>
            </map>
        </property>
    </bean>

    <!-- 核心:XFireExporter 導出 Spring Bean 為 WebService -->
    <bean id="sso" class="org.codehaus.xfire.spring.remoting.XFireExporter">
        <property name="serviceFactory"><ref bean="xfire.serviceFactory"/></property>
        <property name="xfire"><ref bean="xfire"/></property>
        <property name="serviceBean"><ref bean="SSOMethodsImpl"/></property> <!-- 引用 Spring Bean -->
        <property name="serviceClass"><value>com.test.sso.SSOMethodsServices</value></property>
    </bean>
</beans>

3. applicationContext.xml 註冊服務實現 Bean

<bean id="SSOMethodsImpl" class="com.test.sso.impl.SSOMethodsImpl" />
<!-- 其他 Mapper、DataSource 等正常配置 -->

4. 服務實現類保持不變

@Service("SSOMethodsImpl") // 或在 XML 定義
public class SSOMethodsImpl implements SSOMethodsServices {
    @Autowired
    private AccountMapper accountMapper; // 現在可正常注入

    public void someMethod() {
        accountMapper.doSomething(); // 不再為 null
    }
}

原理對比

配置項錯誤方式(原生 XFire)正確方式(Spring 整合)
Servlet 類XFireConfigurableServletXFireSpringServlet
服務配置META-INF/xfire/services.xmlxfire-servlet.xml (Spring Bean)
實例管理XFire 容器Spring 容器
依賴注入不支援完整支援

驗證步驟

  1. 啟動項目,訪問 https://ip:port/project/services 確認服務發布
  2. 用 SoapUI 呼叫服務方法,觀察控制台無 NPE,Mapper 正常執行 SQL
  3. 檢查日誌確認 Spring 容器啟動時已初始化 SSOMethodsImpl Bean

核心原則:讓 Spring 管理服務實例生命週期,XFire 僅負責協議層導出。同理適用於 CXF、Axis2 等 WebService 框架整合 Spring。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved