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.