Source: http://blog.csdn.net/wxy_g/article/details/2071662
Many people have asked me when to create and destroy an object in struts2.0 because it is thread-safe and not a singleton mode?
This is related to the configuration in struts2.0. Let's look at struts. properties.
### if specified, the default object factory can be overridden here### Note: short-hand notation is supported in some cases, such as "spring"### Alternatively, you can provide a com.opensymphony.xwork2.ObjectFactory subclass name here struts.objectFactory = spring
If we use Com. opensymphony. xwork2.objectfactory. To be honest, I have never studied it. xwork has a set of IOC mechanisms like spring, which are small and concise. If you are interested, you can study it. The action in struts2.0 uses this factory mode by default. Let's look at it.
<action name="index" class="hdu.management.action.IndexAction"> <result name="success">/input.jsp</result> <result name="testFTL" type="freemarker">/ftl/test.jsp</result> </action>
The full name of the class must be written to the class attribute. After this configuration, you can identify the lifecycle of the Action object. Anyway, remember that xwork has an object pool, it will allocateIt creates a new instance for each client request. xwork controls the time when the instance is destroyed.
Next, we will use spring to control the lifecycle of an action. I will not go into detail about the integration of action and spring.
<action name="index" class="index"> <result name="success">/input.jsp</result> <result name="testFTL" type="freemarker">/ftl/test.jsp</result> </action>
The class here is the bean ID in the spring configuration file
Let's take a look at the lifecycle section in the spring document.
Table 3.4. Bean scopes
Scope description
Singleton
Scopes a single bean definition to a single object instance per Spring IoC container.
Prototype
Scopes a single bean definition to any number of object instances.
Request
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. only valid in the context of a Web-aware springApplicationContext
.
Session
Scopes a single bean definition to the lifecycle of a HTTPSession
. Only valid in the context of a Web-aware springApplicationContext
.
Global session
Scopes a single bean definition to the lifecycle of a global HTTPSession
. Typically only valid when used in A Portlet context. only valid in the context of a Web-aware springApplicationContext
.