最近在做系统改造的时候,还遇到了一个问题是,如何集成Spring Struts2和Hessian。

当配置Spring和Struts2的时候,在web.xml做了如下配置:

contextConfigLocation
classpath:/spring/*.xml
org.springframework.web.context.ContextLoaderListener

通过设置listener加载Spring的上下文环境,并在struts.xml中设置对象工厂为Spring:

这样,Struts2就可以使用Spring上下文环境中的action bean了。

但在配置Hessian的时候,以前在web.xml中是这样配置的:

Remoting
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:/spring/*.xml
1
Remoting
/remoting/*

在初始化Hessian的servlet的时候又一次把Spring配置文件作为参数,这样又会重新生成一个Spring上下文环境,导致Spring中bean的重复。

为了解决这个问题,在配置Hessian时,做了一下修改,如下:

Remoting
org.springframework.web.servlet.DispatcherServlet
contextAttribute
org.springframework.web.context.WebApplicationContext.ROOT
1

即在初始化Hessian时不再传入Spring配置文件,而是传入通过listener初始化的Spring WebApplicationContext上下文环境,即使用同一个上下文环境。