Struts and Spring integrate Action singleton multi-instance problem

The Action of struts 2 is multi-instance not singleton, that is, an object of Action is generated for each request. The reason is: the Action of struts 2 contains data, for example, the data you fill in the page will be included in the member variable of the Action. If the Action is a single instance, these data will affect each other in a multi-threaded environment, such as causing the data filled in by others to be seen by you. So the Action of Struts2 is multi-case mode.

     The question arises, can the action of struts2 be turned into a singleton mode? When I used spring to generate actions, I found that the generated actions were all singletons. Doesn't this make my program run out of bugs by default? If the information submitted by the previous user is not filled in by the next user, it will go to the information entered by the previous user.

     If the single instance is changed to multiple instances, an exception that the action cannot be found will be reported. It may be that you did not pass the value of the variable you used in the method, and the action error is rather strange.

     Invalid action class configuration that references an unknown class named [adminLogQueryAction]

     Check the parameters submitted by the foreground and the received values ​​in the background, and the values ​​required by the method

Background  :

1) Struts2 will generate an Action instance for each browser to handle.

2) Spring's Ioc container-managed beans are single-instance by default.

First of all, considering the issue of data security, our Action should be guaranteed to be multiple instances, so that there will be no data problems. However, if some actions, such as only admins, can be operated, or some actions are shared by the whole site to improve performance, in this case, the singleton mode can be used.

Fortunately, Spring's bean can set its scope for each, so the above problem is not a problem. If you use multiple instances, set scope="prototype" in spring's action bean configuration. Well, the problem ends here.

Xml code   Favorite code
  1. <!--Multiple cases-->  
  2. <bean id="localFileTaskConFigureAction" class="com.top.fgap.localfiletask.action.LocalFileTaskConAction" scope="prototype">  
  3.         <property name="localFileTaskConfigureService">  
  4.             <ref bean="localFileTaskConfigureService"/>  
  5.         </property>  
  6.     </bean>  
  7. <!--Singleton by default -->  
  8.     <bean id="locFTIpFilterAction" class="com.top.fgap.localfiletask.action.LocFTIpFilterAction">  
  9.         <property name="localIpFilterService">  
  10.             <ref bean="locFTIpFilterService"/>  
  11.         </property>  
  12.     </bean>  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725808&siteId=291194637