Dependency Injection in Struts2
“You do not create object, describe how to use objects.
The container creates the required object whenever is required.”
The
Dependency Injection helps in removing the task of creation of objects in the
source code. The IoC uses the factory design pattern. The default factory used
in Struts 2 is ObjectFactory. Inversion of Control (IoC) is based on how
services are defined and how they should locate with the other services on
which they depend.
To obtain the traditional J2EE objects whenever need in Action, Struts 2 provides Dependency Injection and Inversion of Control in the form of aware interface. The Aware interfaces are injected in the action class.
The Struts 2 framework the following aware interfaces to obtain the ServletContext,HttpServletRequest, HttServletResponse, HttpSession, Cookie:
To obtain the traditional J2EE objects whenever need in Action, Struts 2 provides Dependency Injection and Inversion of Control in the form of aware interface. The Aware interfaces are injected in the action class.
The Struts 2 framework the following aware interfaces to obtain the ServletContext,HttpServletRequest, HttServletResponse, HttpSession, Cookie:
Aware Interface
|
Description
|
ApplicationAware
|
To
set the Map object in ServletContext object
|
CookiesAware
|
To
obtain all the cookies
|
ServletContextAware
|
To
obtain J2EE ServletContext objectr
|
RequestAware
|
To
obtain current servlet request attributes
|
ServletRequestAware
|
To
access the current servlet request(HttpServletRequest)
|
ServletResponseAware
|
To
access the current servlet response(HttpServletResponse) and associate map
with it
|
SessionAware
|
To
access J2EE session objects and associate map with it
|