最近在作系统改造的时候,还遇到了一个问题是,如何集成Spring Struts2和Hessian。web
当配置Spring和Struts2的时候,在web.xml作了以下配置:spring
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/*.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
经过设置listener加载Spring的上下文环境,并在struts.xml中设置对象工厂为Spring:app
<constant name="struts.objectFactory" value="spring" />
这样,Struts2就能够使用Spring上下文环境中的action bean了。ide
但在配置Hessian的时候,之前在web.xml中是这样配置的:url
<servlet> <servlet-name>Remoting</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Remoting</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping>
在初始化Hessian的servlet的时候又一次把Spring配置文件做为参数,这样又会从新生成一个Spring上下文环境,致使Spring中bean的重复。spa
为了解决这个问题,在配置Hessian时,作了一下修改,以下:xml
<servlet> <servlet-name>Remoting</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <!-- <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/*.xml</param-value> --> <!-- 该servlet的spring上下文采用WebApplicationContext,再也不重复生成上下文 --> <param-name>contextAttribute</param-name> <param-value> org.springframework.web.context.WebApplicationContext.ROOT </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
即在初始化Hessian时再也不传入Spring配置文件,而是传入经过listener初始化的Spring WebApplicationContext上下文环境,即便用同一个上下文环境。
对象