Versions Compared

Key

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

...

For a Maven application you'll need to a add dependency to the struts2-spring-plugin jar (see the Maven example application for this tutorial). For a an Ant built application you'll need to add the struts2-spring-plugin jar and the Spring jars to your application's class path (see the Ant example application for this tutorial).

...

To make our application "Spring aware" we need to add this line to web.xml.

Code Block
XML
XML
1titleSpring Listener In web.xml

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

The above code will activate the Spring framework when the application is started up by the Servlet container. By default Spring will look for a configuration file name applicationContext.xml in WEB-INF (consult the Spring documentation for how you can change where Spring looks and what the configuration file name is).

Spring Configuration File

In the Spring configuration file we create a bean node for those objects we want Spring to create an instance of and inject into our ActionSupport class. In the example application is this applicationContext.xml.

Code Block
XML
XML
titleSpring Configuration File


<?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" />

</beans>

Note the id value above. By default the Spring plugin works with Spring to autowire the dependencies of the ActionClass by "name." Spring will create an object of class EditServiceMemory and provide that object to any ActionSupport class that has a setEditService method with an argument of type EditService. Consult the Spring Plugin documentation for how to change the default autowire method.

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).