---
title: "struts + spring + xfire (组合)"
author: "hypgr"
date: "2008-08-15T10:14:12Z"
tags: []
slug: "struts-spring-xfire"
draft: false
lastmod: "2008-08-15T10:14:12Z"
id: 228235
url: "https://hypgr.iteye.com/blog/228235"
---

一般情况下， spring+ struts的组合，struts 作为web展现层， 现在加xfire。其实，细心的朋友在做这个组合的时候，在配置的时候会出现一个问题。struts通过struts-config.xml文件中的 plugin与spring进行连接，而xfire通过在web.xml，配置一个listener和一个servlet与 spring 通信。struts会load一遍配置文件，listener也会load一遍配置文件。这样就会引起冲突。

我在做配置的时候，就是出现了上面的问题。两次load配置文件的问题。我让struts里，加载dao,service,transaction 层的配置文件，web.xml加载xfire文件。然后，启动web工程，开始是正常的。struts+spring+ibatis的正常网页，一切正 常。但是刚把service注入进xfire的时候，出现了无法加载的错误，也就是空指针。本来嘛，xfire和struts就是分着加载的。当时还想着 是两次启动造成了spring的两个实例，两个实例之间不能共享。

然后，尝试了N次，最后终于找到解决办法了。

struts里，plugin这样写，  value=""

 

```
<plug-in
 className="org.springframework.web.struts.ContextLoaderPlugIn">
 <set-property property="contextConfigLocation" value="" />
</plug-in>
```

 web.xml里，统一进行加载。

```
   1.  <context-param></context-param><context-param>
   2. <param-name>contextConfigLocation</param-name>
   3. <param-value>/WEB-INF/ApplicationContext.xml</param-value>
   4. </context-param>
   5. <listener></listener>
   6. <listener-class></listener-class>
   7.       <listener>
   8. <listener-class>
   9. org.springframework.web.context.ContextLoaderListener
  10. </listener-class>
  11. </listener>
  12. <servlet>
  13. <servlet-name>XFireServlet</servlet-name>
  14. <servlet-class>
  15. org.codehaus.xfire.spring.XFireSpringServlet
  16. </servlet-class>
  17. <load-on-startup>0</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>action</servlet-name>
  21. <url-pattern>*.do</url-pattern>
  22. </servlet-mapping>
  23. <servlet-mapping>
  24. <servlet-name>XFireServlet</servlet-name>
  25. <url-pattern>/services/*</url-pattern>
  26. </servlet-mapping>

```

 这样子，struts 和xfire就可以一起启动了。 因为xfire一定要通过web.xml启动。