Struts2 - Dependency Injection and Inversion of Control

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.

public class MyDaoObject
{
protected DataSource datasourceObject =new DataSourceImpl("driver","url","user","password");
//methos to access data
public Employee readEmployee(int primaryKey){...}
}

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

package yusata;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import com.opensymphony.xwork2.ActionSupport;

public class ApplicationAwareAction extends ActionSupport implements ApplicationAware
{

private static final long serialVersionUID = 1L;
Map aps;
@Override
public void setApplication(Map aps) {
// TODO Auto-generated method stub
this.aps=aps;
}

public String execute() throws Exception
{
aps.put("name", "struts 2 blog");
return SUCCESS;
}
}

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

< s:property value="#application.name"/>
or
< s:property value="#application['name']"/>

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

package yusata;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;

public class SessionAwareSample extends ActionSupport implements SessionAware
{
Map sessions;
@Override
public void setSession(Map sessions) {
// TODO Auto-generated method stub
this.sessions=sessions;
}
public String execute() throws Exception{
sessions.put("id", "JAVA STUDY CHANNEL.. learn over a cofee");
return SUCCESS;
}
}

The single key /value pair stored in session Map session can be accesses in a JSP as shown below.

Example

< s:property value="="#session.id"/>
or
< s:property value="="#session['id']"/ >

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

package yusata;
import java.util.Map;
import org.apache.struts2.interceptor.ParameterAware;
import com.opensymphony.xwork2.ActionSupport;

public class SessionAwareSample extends ActionSupport implements ParameterAware
{
Map params;
@Override
public void setParameters(Map params) {
// TODO Auto-generated method stub
this.params=params;
}

public String execute() throws Exception
{
return SUCCESS;
}
}

The parameter Map contains all the input values and the action class can use this map to process input parameters.

Example

< s:property value="#parameters.id"/>
or
< s:property value="#parameters['name']"/ >

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

package yusata;
import java.util.Map;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionAwareSample extends ActionSupport implements ServletRequestAware
{
HttpServletRequest reqt;

@Override
public void setServletRequest(HttpServletRequest reqt) {
// TODO Auto-generated method stub
this.reqt=reqt;
}

public String execute() throws Exception
{
reqt.setAttribute("site","strtus 2 blog");
HttpSession session=reqt.getSession();
return SUCCESS;
}
}

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

package yusata;
import java.util.Map;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletResponse;
public class SessionAwareSample extends ActionSupport implements ServletResponseAware
{
HttpServletResponse resp;
@Override
public void setServletResponse(HttpServletResponse resp) {
// TODO Auto-generated method stub
this.resp=resp;
}

public String execute() throws Exception
{
resp.setContentType("text/html");
int buf=resp.getBufferSize();
return SUCCESS;
}
}

0 comments:

Post a Comment