Inversion of Control/Dependency Injection are still big buzz words. Popularity continues to grow for IoC "containers", notably:
In general these reduce couplings between components, letting the IoC container piece your objects together. Struts has some built-in functionality for supporting IoC on data sources, form beans and actions.
Data Sources
A data source can be configured in the struts-config.xml file and accessed through the base Action class's getDataSource() method. A concrete implementation of a datasource can be retrieved through a symbolic name. See Struts - Accessing a database for more information.
The scope of these objects is Global, only one is created per Application.
Every data source is allocated at application startup weather it is used or not.
Form Beans
Form Beans are either custom Java classes (with simple getters and setters for data properties) or generic objects (like Maps or Dyna*Beans). Generic objects can be configured with a list of properties and constraints with default values on those properties, but custom Java classes cannot be configured at all.
The scope of these objects is determined by the Action using the Form Bean. They can be easier Session or Request.
Form Beans are allocated as they are needed.
Actions
Actions map a symbolic name to a concrete class. This class is configured with a form bean, a set of mappings and a generic String parameter.
The scope of these objects is Global, only one is created per Application.
Actions are allocated as they are needed and cached until the next use.
Analysis
The actions are the work-horses in struts. They need to interact with business logic classes, they almost always get a user profile object out of the session. The tight coupling that currently exists in struts actions make them difficult to unit test and reconfigure.
Struts has a fair amount of logic hardcoded in ActionServlet and RequestUtils for performing this Inversion of Control. If this were abstracted out into a plugable helper class, the core struts code could decrease while allowing other projects to maintain the burden of this work.
A new interface called IoCAdaptor could be created, along with three implementations of this interface:
- StrutsBuiltinIoCAdaptor
- PicoIoCAdaptor
- SpringIoCAdaptor
One of these adaptors could be installed into the servlet context by configuring a struts plug-in in the struts-config.xml.
Recently Spring added support for Struts integration. This support came in two forms:
- Proxy delegates to Spring-managed Action
- Action has easy access to a Spring context
While this is a nice advance over no integration, I'd still like to see work done at the core of Struts. This would improve the following issues:
- The IoC code could evolve separately from the struts core.
- IoC could be supported for Form and DataSources, not just Actions.
- No need for a special proxy action -- Integration would be more natural.
[Note: This entry was originally written back in December 2003. Webwork and Spring among other UI frameworks fully support dependency injection.]
<< Home