Dependency Injection In Struts2

The Inversion of Control (IoC) and Dependency Injection (DI) are programming design pattern, which is used to reduce compiling in computer programs. 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:
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


Let's demostrate all common aware interface here :

Previous Next

Comment by : sadeesh May 20th, 2015
Comment : I appreciate your effort to made this website .Especially the struts2 article is wonderful and it is very useful for me .every thing is explained with source code is fantastic .After reading this stru

Comment by : Aman May 27th, 2014
Comment : Nice Tutorial for beginners and Intermediate developer....

Comment by : Nagesh April 28th, 2014
Comment : best website so many doubts i have clarified

Comment by : Varsha Babar January 22nd, 2014
Comment : Very good article

Comment by : Varsha Babar January 22nd, 2014
Comment : Very helpful article.

Comment by : anas January 22nd, 2014
Comment : good tutorial

Comment by : ANAS January 11th, 2014
Comment : Very good tutorial

Comment by : jagamohan sahoo January 6th, 2014
Comment : good website

Comment by : selvam January 4th, 2014
Comment : very nice artical

Comment by : vijaya December 21st, 2013
Comment : nice articlee which is very understandable n clear

Comment by : richard December 21st, 2013
Comment : good

Comment by : C Naresh Reddy December 21st, 2013
Comment : Nice tutorial for beginners and Intermediate level along with simple examples.Thanks a lot.Keep sharing best practices to use framework 100%

Comment by : arun sharma December 20th, 2013
Comment : gud content

Comment by : arun December 20th, 2013
Comment : gud content........

Comment by : arun December 20th, 2013
Comment : gud content

Comment by : polraj December 18th, 2013
Comment : This is very good tutorial to learn with example code

Comment by : arun sharma December 15th, 2013
Comment : good example

Comment by : arun sharma December 15th, 2013
Comment : very easy way to explain the concept of struts2 framework..... thanks dud

Comment by : arun sharma December 15th, 2013
Comment : Very easy way to explain the concept of basic struts concept...thanks dud....

Comment by : Rajat December 13th, 2013
Comment : best website...all my doubts are cleared nw..thnks a ton

Comment by : venkateswararao December 4th, 2013
Comment : good

Comment by : Remmya Nair December 3rd, 2013
Comment : Nice

Comment by : mari November 25th, 2013
Comment : Nice

Comment by : Dhana November 21st, 2013
Comment : This site is an awesome. Help great in understanding the core concepts in detail.

Comment by : vivek November 13th, 2013
Comment : hi

Comment by : prerit October 28th, 2013
Comment : liked the way ,topic was explained.

Comment by : alessandro October 19th, 2013
Comment : very helpful, thank you

Comment by : amit October 14th, 2013
Comment : Words are less to describe about this article.each concept is being covered very well.this is too good i loved the article as a beginner as well as intermediate developer.thanks for such a good articl

Comment by : Sathyaraj A October 14th, 2013
Comment : Nice


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code