Versions Compared

Key

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

...

Spring provides a mechanism to manage dependencies by injecting them at run time. Struts 2 ActionSupport classes--like any other Java class-can be injected with a dependent object by the Spring framework. So instead of having the above code, I would have this statement in EditAction.

Code Block
JAVA
JAVA
titleEditAction Class No Hard-Coded Dependency

    private EditService editService ;

...

Struts 2 provides a plugin that enables Spring to inject into the ActionSupport classes any dependent objects you've specified in the Spring configuration file. Consult Spring Plugin documentation for more information about how the plugin works.

For a Maven application you'll need to add a dependency to the struts2-spring-plugin jar (see the Maven example application for this tutorial). The plugin's pom.xml includes transitive dependencies to the Spring jar files. For 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).

...