Detailed description of the object lifecycle of action in struts2.0 !!

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.