ignore

Dependency Injection Annotations

Dependency Injection Annotations

A powerful feature of Strecks is the ability to populate action bean properties with dependencies. Dependencies range from those specific to a request to application-wide dependencies. The extensible dependency injection framework allows dependencies to be resolved from any object accessible via the HttpServletRequest (and hence HttpSession), the HttpServletResponse, the ServletContext, the Struts ActionForm and Struts ActionMapping.

Applying the dependency injection annotations is very straightforward. Simply add the annotation to an action bean setter method, and add any necessary attributes. An example is shown below:

@Controller(name = BasicSubmitController.class)
public class SubmitNewBookingAction implements BasicSubmitAction
{

	private HolidayBookingForm form;

	private HolidayBookingService holidayBookingService;

	private WebHelper webHelper;

	public void preBind()
	{
		form.setBooking(new HolidayBooking());
	}

	public String cancel()
	{
	  //implementation omitted
		return "success";
	}

	public String execute()
	{
	  //implementation omitted
		return "success";
	}

	@InjectActionForm
	public void setForm(HolidayBookingForm form)
	{
		this.form = form;
	}

	@InjectSpringBean(name = "holidayBookingService")
	public void setHolidayBookingService(
				 HolidayBookingService holidayBookingService)
	{
		this.holidayBookingService = holidayBookingService;
	}

	@InjectWebHelper
	public void setWebHelper(WebHelper webHelper)
	{
		this.webHelper = webHelper;
	}

}

In the next section we describe the dependency injections annotations currently available.

@InjectActionForm

Allows the ActionForm currently associated with the request to be injected into an action bean property. May be null.

@InjectActionMapping

Allows the ActionForm currently associated with the request to be injected into an action bean property. Will never be null.

@InjectContext

Allows the application's ServletContext to be injected into an action bean property. Will never be null.

@InjectContextAtttribute

Allows a named ServletContext attribute to be injected into an action bean property. May be null.

Parameters

name Specifies the name of the attribute. Defaults to the JavaBean property name
autocreate Whether to instantiate an object if none is present. Default is false. Requires injected type to be a class with a no-arguments constructor

@InjectLocale

Injects the current request Locale into the action bean.

@InjectMessageResources

Injects a set of message resources into the action bean. May return null.

Parameters

bundle The name of the resource bundle. Defaults to default resource bundle, if one is available.

@InjectMessageResourcesHelper

Injects a MessageResourcesHelper instance into the action bean. The MessageResourcesHelper wraps the MessageResources for the named bundle, as well as the current request locale. It exposes two methods for retrieving messages from the message resources for the current request locale.

public String getMessage(String key);
public String getMessage(String key, Object... params);

Note that the second mechanism uses the Java 5 varargs mechanism to allow for an arbitrary number of message parameters.

Parameters

bundle The name of the resource bundle wrapped by the MessageResourcesHelper instance. Defaults to default resource bundle, if one is available.

@InjectRedirectHelper

Injects a RedirectHelper object into the action bean. RedirectHelper is useful for implementing the Redirect After Post pattern in the context of a form submission. Will not be null.

@InjectRedirectParameter

Injects a named redirect parameter object into the action bean. May be null. Redirect parameters are session attributes which only remain in the session for the lifetime of a single redirect, and are useful in propogating state during a redirect after post.

@InjectRequest

Injects the current HttpServletRequest into the action bean. Will never be null.

@InjectRequestAttribute

Allows a named HttpServletRequest attribute to be injected into an action bean property. May be null.

Parameters

name Specifies the name of the attribute. Defaults to the JavaBean property name
autocreate Whether to instantiate an object if none is present. Default is false. Requires injected type to be a class with a no-arguments constructor

@InjectRequestParameter

Injects a named HttpServletRequest parameter into the action bean. If necessary, the value will be type converted using the specified or implicit converter, allowing for non-String request parameters to be bound to the action bean without code.

Parameters

name Specifies the name of the parameter. Defaults to the JavaBean property name
converter The Converter class to be used for type conversion. Default is StandardBeanUtilsConverter, which can handle many basic type conversions
required Whether the request parameter must be present. Defaults to false. If true, then a missing request parameter value generates a runtime exception and should be interpreted as a programming error

@Inject Response

Injects the current HttpServletResponse into the action bean. Will never be null.

@InjectScopedAttribute

Allows a named HttpServletRequest, HttpSession or ServletContext attribute to be injected into an action bean property. Searches for the attribute in the order just specified. May be null. Useful when the attribute may exist in more than one of the above-named scopes.

Parameters

name Specifies the name of the attribute. Defaults to the JavaBean property name

@InjectSessionAttribute

Allows a named HttpSession attribute to be injected into an action bean property. May be null.

Parameters

name Specifies the name of the attribute. Defaults to the JavaBean property name
autocreate Whether to instantiate an object if none is present. Default is false. Requires injected type to be a class with a no-arguments constructor
required Whether the session attribute must be present. Defaults to false. If true, then a missing session attribute generates a runtime exception and should be interpreted as a programming error

@InjectSpringBean

Allows a named Spring bean to be injected into an action bean property. Cannot be null. If so, a runtime exception will be thrown, indicating a programming error, resulting either from the incorrect Spring name or an error in the Spring configuration.

Parameters

name Specifies the name of the Spring bean. Defaults to the property name.

@InjectWebHelper

Injects a WebHelper object, which is useful in allowing access to Servlet API operations without having to use the Servlet API directly, simplyfying testing of action beans.

SourceForge.net logo java.net Member logo Copyright © 2005-2007 Realsolve Solutions