---
title: "使用spring 的action-servlet.xml解决struts线程问题"
author: "hypgr"
date: "2008-08-15T10:16:11Z"
tags: []
slug: "spring-action-servlet-xml-struts"
draft: false
lastmod: "2008-08-15T10:16:11Z"
id: 228236
url: "https://hypgr.iteye.com/blog/228236"
---

&lt;bean name="/register" class="com.action.RegisterAction"     singleton="false"&gt;

&lt;property name="businessService"&gt;  
     &lt;ref bean="businessService"/&gt;  
   &lt;/property&gt;

&lt;/bean&gt;

    RegisterAction是Action的实现类，businessService是业务逻辑，Spring把businessService注入到 Action中，在Action中只要写businessService的get和set方法就可以了，同时action的bean设为 singleton="false"，这样每次新建一个实例，从而解决了Struts中Action的线程同步问题

1.struts 中实现接口

   &lt;plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"&gt;  
     &lt;set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml" /&gt;  
   &lt;/plug-in&gt;

2.将&lt;action type 变为

= "org.springframework.web.struts.DelegatingActionProxy"

3.编写action-servlet.xml 如：

   &lt;bean name="/Analyse" class="com.apple.struts.action.IndexAnalyseAction" singleton="false"&gt;  
     &lt;property name="flowService"&gt;  
       &lt;ref bean="flowService"/&gt;    //此处对应applicationContext.xml  
     &lt;/property&gt;  
   &lt;/bean&gt;

4.web.xml中配置

&lt;filter&gt;    
   &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;    
   &lt;filter-class&gt;    
      org.springframework.orm.hibernate3.support.OpenSessionInViewFilter     
   &lt;/filter-class&gt;    
   &lt;init-param&gt;  
     &lt;param-name&gt;singleSession&lt;/param-name&gt;  
     &lt;param-value&gt;true&lt;/param-value&gt;  
   &lt;/init-param&gt;  
&lt;/filter&gt;

&lt;filter-mapping&gt;    
   &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;    
   &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;    
&lt;/filter-mapping&gt;