Struts2 - Dependency Injection and Inversion of Control
Dependency injection is a phenomena of configuring object in which fields of objects and collaborators are set by an outside entity. Clearly we can say an external entity configures objects. It can be said as an alternative procedure to configure the object by itself. Let us go through a simple example to understand this.
In the above Data Access Object (DAO) class, MyDaoObject, using an instance of javax.sql.DataSource in order to get connections to the database. For Employee object instance, connections to the database are used to write the data to the database and read from the database.
You might have notice that how MyDaoObject class instantiates a instance of DataSourceImpl as the DataSource required. Here the truth is that the MyDaoObject class requires a DataSource implemenation by which it is depending on it. It is not possible to complete its work without implementation of DataSource. Hence, MyDaoObject has a DataSource interface "dependency" and it is depending on some implementation of it.
This explains how coupling is going on objects. we can say dependency or coupling is the level to which two or more program module or objects relies on each other.
Now Let us come back to the dependacy injection and Inversion of control.Dependacy Injection (DI) and Inversion of Control are the design patterns, by using which we can reduce the dependancy or coupling in our programs. Dependancy Injection and Inversion of Control follows the two principles given below:
You do not need to Create Objects. You have to describe how they should be created.There is no need to instantiate or locate directly the services required by components. Instead of which we have to describe which services are required by which components.
Aware Interfaces in Struts2
When a HTTP specific object is required in Action, Dependancy Injection and Inversion of control is used in Struts 2. For this technique some interfaces known as aware interfaces will be injected into the action class. These interfaces are known as aware Interfaces. These Interfaces names will ends with Aware, that is why theses are known as aware Interfaces. Aware Interfaces contains methods to set specific resources into the action class.
In Struts 2 Dependancy Injection pattern impements the required interface, which exposes some methods to the action class.
Some of the aware interfaces which supports Struts 2
- Application Interfaces
- SessionAware Interface
- ParameterAware Interface
- ServletRequestAware Interface
- ServletResponseAware Interface
ApplicationAware Interface
Package - org.apache.struts2.interceptor
Use - To expose a method to action class which sets an Application Map object in action class.
Methods - setApplication() which sets the map of application attributes in class.
Sample implementation of ApplicationAware interface
All key / value pairs which are stored in Application Map can be accessed from any JSP page.Struts2 jsp tags support the display of different application scope using corresponding key.
Example
SessionAware Interface
Package - org.apache.struts2.interceptor
Use - is used handle the client session with in our action Action.
Methods - setSession() which sets the map of session attributes in class.This interface can access the session attributes in the action class.
Sample implementation of SessionAware interface
The single key /value pair stored in session Map session can be accesses in a JSP as shown below.
Example
ParameterAware Interface
Package - org.apache.struts2.interceptor
Use - is used when input parameters are to be handled by the Action Class.
Methods - setParameters() which sets the map of parameter attributes in class.This interface can access the session attributes in the action class.
all the parameters for a given name will return a string data,so the type os object data in the Map is String[].
Sample implementation of ParameterAware interface
The parameter Map contains all the input values and the action class can use this map to process input parameters.
Example
ServletRequestAware Interface
The HttpServletRequest object is not acilable in the action.But this can be injected in the action class with ServletRequestAware Interface. >
Package - org.apache.struts2.interceptor
Use - is allows an action to use HttpServletRequest in a servlet environment.
Methods - ServletRequest(HttpServletRequest reqt)
Sample implementation of ServletRequestAware interface
ServletResponseAware Interface
The ServletResponseAware Interface is similar to the ServeletRequestAware interface except that object is injected HttpServletResponse action. >
Package - org.apache.struts2.interceptor
Use - is allows an action to use HttpServletResponse in a action class.
Methods - ServletResponse(HttpServletResponse resp)
Sample implementation of ServletResponseAware interface
0 comments:
Post a Comment