spring scope="prototype" study notes

<bean id="meetAction" class="com.web.actions.MeetsAction"
  scope="prototype">
  <property name="meetsService" ref="meetsService" />
 </bean>

 <!--
 scope="prototype" is not written, the operation of adding or deleting a table in the project is to use an action, this action has add, update, delete, save these methods,
 adding and modifying share a page, when When the page gets the id, it represents the modification operation, and vice versa. Because I forgot to write scope="prototype" when configuring the spring bean, the
 last accessed record is displayed every time it is added, and scope="prototype" will
 create a new action object when an object of this type is requested. If scope=prototype is not configured, it will not create a new action when adding it, it will still keep the recorded information of the last visit.
The action of the webwork is not thread-safe, and it is required that in a multi-threaded environment, a thread must correspond to an independent instance, singleton cannot be used. Therefore, when we configure the Webwork Action Bean in Spring, we need to add the attribute scope=”prototype” or singleton=”false”.
The singleton mode refers to the complete sharing of an object, including code space and data space. To put it bluntly, if a class is singleton, if the class has a member variable, then the value of the member variable is shared by each thread (somewhat Similar to static), when thread A assigns a value to the variable, thread B can read the value. Therefore, for the foreground Action, the singleton mode must not be used. It must be a thread request corresponding to an independent instance. By extension, as long as it is a class with data member variables, in order to prevent multiple threads from mixing data, singleton cannot be used. For the Service and Dao we use, the reason why we use singleton is because they do not use data member variables. If someone's Service needs data member variables, please set singleton=false.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326678020&siteId=291194637