Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Tip

The editService bean created by Spring will have a scope of singleton since that is the default scope. Consult section 3.5 of the Spring documentation for how to configure the bean definition to use a different scope (e.g. request or session).

Alternative - Have Spring Manage Creation Of ActionSupport Class

Using the above methodology, the Struts 2 framework will still manage the creation of the ActionSupport class. If you prefer you can configure the application so that Spring will create the ActionSupport class also. To support this technique you need to add a bean node to the Spring configuration file for the ActionSupport class.

Code Block
XML
XML
titleSpring Configuration For ActionSupport Class Managed By Spring


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
            

<bean id="editService" class="org.apache.struts.edit.service.EditServiceInMemory" />

<bean id="editAction" class="org.apache.struts.edit.action.EditAction" >

	<property name="editService" ref="editService" />
	
</bean>

</beans>

Note in the above that there is an editAction bean and its editService property is set to the editService bean. Since we are having Spring manage the EditAction class we must specify any properties of EditAction that we want Spring to inject.

In the struts.xml configuration file you must specify the Spring id value for the class attribute of the action node. This tells Struts to get a bean with that id value from Spring for the Action class.

Code Block
XML
XML
1Struts Configuration For Spring Managed ActionSupport Class


<action name="edit" class="editAction" method="input">
	<result name="input">/edit.jsp</result>
</action>