Struts.objectfactory action instance from spring management struts2

Source: Internet
Author: User


When the specified struts.objectfactory is spring, the STRUTS2 framework sends the bean to spring to create, assemble, inject. However, after the bean has been created, the Struts container manages its lifecycle. Configuration mode:

(1) in Struts.xml:

XML code

<constant name= "struts.objectfactory" value= "Spring"/>

(2) Struts.properties: Java code

Struts.objectfactory=spring/org.apache.struts2.spring.strutsspringobjectfactory

Typically, that's enough, and then in struts ' action-mapping configuration file, the following: XML code

<action name= "user" class= "com.hafx.codesure.tqs.rbac.action.UserAction" ></action>


can be injected automatically if there are other beans that are dependent on the action. At this point, the action instance is created in prototype, and spring creates an instance of the action for each request.

The following presentation configuration uses Jprofiler to monitor the number of Useraction instances:




With the above monitoring, you will find that each time you perform a request, a new instance of Useraction is created.


At some point, you might want to let spring not only create and assemble the action objects, but also want spring to fully manage those objects, such as when you want to use AOP or when you want to use Acegi. At this point, you simply define these action in the spring configuration file. As in the Applicationcontext.xml file: XML code

<bean id= "user" class= "com.myapp.web.action.user.UserAction"/>

Then, in action-mapping, specify class= "user".

One place to note: The default scope in spring is the singleton scope. And often the action belongs to a stateful bean, you should use the prototype scope. <bean id= "user" class= "com.myapp.web.action.user.UserAction" singleton= "false"/> If this detail is omitted, in our convenient practical set, When a Get method processes data, it may be at risk of thread safety.

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.