Constructor Injection



Que: -How is constructor injection implemented in a spring application?

Step 1: – Declare the dependency as instance variable in the dependent bean.

e.g.: - public class HelloBean //dependent
String message; //dependency

Step 2: – Define a parameterize constructor in the dependent bean.

e.g.: - public HelloBean (String m)
Message m;

In this parameterize constructor initialize the dependency variable.

Step 3: – In the bean configuration file, while configuring the dependent bean make use of <constructor-arg > tag.

e.g.: - <constructor –arg value= “Hello world” />

Note: – In the above tag various attributes are there to deal with overloaded constructor with any number of arguments without ambiguity.

If the dependency is a user defined variable i.e. a reference, we use “ref” attribute instead of value attribute to populate the dependency.

Que: -How to get spring core container in a spring application?

org .springframework.beans.factory.BeanFactory” is an interface implementation class of this interface is nothing but spring core container or IOC container.


XmlBeanFactory is one of the few implementations of BeanFactory interface. This is the most widely used spring container class. If we instantiate XmlBeanFactory class, it is nothing but spring container. XmlBeanFactory constructor takes “” interface object as argument. Resource interface has many implementation class of which the following are widely used:
If we use FileSystemResource object we need to specify the bean configuration file absolute path of the file system. If we use ClassPathResource bean configuration file should be placed in classpath. Resource object provides bean configuration file to the BeanFactory. We get the spring container in a spring application as follows:


BeanFactory factory = new XmlBeanFactory(new ClassPathResource(“beans.xml”));

factory” is a reference referring to spring container object. Spring container object needs the configuration file to load the bean definition which is provided by Resource object. Factory design pattern is used in the implementation of spring container. When the BeanFactory instance is created only spring bean definitions are loaded into memory but spring beans are not instantiated. When getBean() method is called on the factory instance, at that time spring bean instance is created by the spring container and the bean references is returned.


Share Button

Core module of spring framework



Que: – What is core module of spring framework?

Core module is the base of spring framework. Core module is also known as IOC module. Inversion of control module is Nothing but spring container. Spring container is mainly responsible for bean life cycle management and providing dependencies to the spring beans.

Que: – What are the main principles of spring framework?

In spring application development two features are mainly used:

  1. IOC (Inversion of control)
  2. AOP

Que: -what is IOC (Inversion of control)?

Inversion of control is a designing principle which is purely conceptual in which, an external agency (entity) providing dependencies application components instead of hand coding or hard coding them in the component code. IOC principle is famously known as Hollywood principle, i.e. “Don’t call us, we will call u”.

Que: -what is DI (Dependency Injection)?

Physical realization of the theoretical principle called IOC is nothing but dependency injection (DI)”.In case of spring applications, spring container is the external entity that provides dependency object references into dependent objects through the feature known as dependency injection (DI).

We have two kind of injection: –

  1. Setter injection
  2. Constructor injection

Note: –Interface injection (method injection) is also available for used.

Que: -How to implement setter injection?

Step 1: –Declare the dependency as the instance variable in the spring bean class.

e.g.: - public class serviceBean
DAOBean dao;

Step 2: – Ensure public zero argument constructor in the dependent class.

Step 3: – Develop the public setter method to the dependency variable in the dependent class.

e.g.: - public void setDAO (DAOBean d)

In this method we assign the parameter value to the dependency variable. This setter method is called by spring container implicitly.

Step 4: – In the bean configuration file make use of <property>

Tag value configuring the dependent bean.

e.g.: - <property name= “dao” ref= “otherbean id”/>


<property name= “message” value= “Helloworld”/>

Note: – If the dependency is a primitive variable or string variable, we populate it using “value” attribute. If the dependency variable is a reference variable of any user defined type, “ref” attribute of <property> tag is used to populate the dependency variable.


Share Button

Spring framework application development



Q. Modify the previous application so as to have a constructor in “HelloBean” class instead of setter method.

Note: is as it is from previous application.


public class HelloBean
private String message;
public HelloBean (String message)
this.message= message;
public void displayMessage(){
System.out.println("Hello world"):
}// business method


<bean Id= "hb" class= "HelloBean">
<constructor-org value="HelloWorld"/>

import org.springframrwork.beans.factory.xml.XmlBeanFactory;

class ExecuteBean
public static void main (String [] args)
XmlBeanFactory container= new XmlBeanFactory(new FileSystemResource("beans. xml"));
HelloBean mybean = (HelloBean)container.getBean("nb");

Note: while configuring a spring bean in the beans configuration files, we use <property> tag to populate bean field if setter method is specified in the bean class.

In the bean class if the constructor is used, to initialize the bean field, we use <constructor-org> tag while configuring the spring bean in bean configuration file.

Q. Develop a spring application in which controller-bean communicates with service-bean and service-bean communicates with DAO-bean.


public class DAOBean
public void findAccount()
System.out.println("Account details given by DAO Bean");
}// Data access layer spring bean source code

class ServiceBean // dependent
DAOBean dao; // dependency
public void setDAO(DAOBean dao)
this.dao = dao;
}// setter injection
public void calculateInterest()
System.out.println ("interest calculated in service layer");
}//business method

public class ControllerBean // dependent
ServiceBean service;// dependency
public void setService(ServiceBean service)
this.service = service;
public void execute()
System.out.println ("Presentation layer code executed");


<bean Id= "db" class = "DAOBean"/>
<bean id= "sb" class = "ServiceBean">
<property name = "dao" ref="db"/>
<bean id= "cb" class = "ContrallerBean">
<property name= "service" ref= "sb"/>

import org.springframrwork. beans.factory.xml.XmlBeanFactory;

class ExecuteBean
public static void main ( String [] args)
XmlBeanFactory container= new XmlBeanFactory(new FileSystemResource("beans.xml"));
ControllerBean mybean = (ControllerBean)container.getBean("cb");


Share Button

Dependency resolution in Spring application


“Dependencies are injected into dependents through dependency injection by Spring container, this is known as dependency resolution.”

What are the system requirements to develop spring application ?

To develop and run spring application we need to install the following in computer system:

  1. J2SE platform (jdk 1.6 and above)
  2. Install spring files and place them in class path.
  3. Any Web container (Tomcat 6.x).

How to develop spring application ?

Step 1: According to application requirement develop the spring beans (POJO classes).

Step 2: Develop a beans configuration file it is a text file with any name but with “.xml” extension ex: beans.xml. Spring container uses this files to control the life cycle of the spring beans and understand their dependencies.

spring container

Step 3: Create a spring container instance.

step 4: Which spring bean method we want to call, get the references of those beans from the spring container. Make the method calls on the spring beans.

Develop and run a spring application in which a spring bean displays “Hello World” message on the console upon its method call.

hello world 

public class HelloBean
private String message;
public void stMessage(String message)
public void displayMessage(){


<bean id="hd" class= "HelloBean">
<property name="message" value="HelloWorld"/>

import org.SpringFramework.beans.factory.xml.xmlBeanFactory;
class ExecuteBean
public static void main(String [] args)
XmlBeanFactory container=new XmlBeanFactory(new FileSystemResource("beans.xml")); //Spring container created
HelloBean mybean= (HelloBean)container.getBean("hd");

Note: place two jar files in class path before you compile and run this program.

  1. spring. jar
  2. Common-logging.jar


Share Button

Architecture of Spring Application


Architecture of Spring Application:

If Spring is used in all layer of enterprise application, then the spring application is as follows.

Spring architecture

In spring application the following classes are used in each layer

  1. Presentation Layer: DispatcherServlet, Controller Classes, JSP’s
  2. Business Logic Layer: Business Object/Service Classes, Value Object Classes
  3. Data Access Layer: DAO Classes, Domain Object Classes

DispatcherServlet is the entry point into a spring application. For any kind of user request it always control to DispatcherServlet, It is known as front controller. DispatcherServlet uses controller classes as assistance for each service offered by the application. For each task of the application one controller class is used, these controller class are spring beans. For each task the controller calls the business method of the service layer class. Service layer classes are also spring beans running in the spring container. Depending upon the requirement of project and use case we develop the business object class or Service class. Service layer classes make use of DAO classes for database data. DAO classes are also Spring beans. Controller beans are dependent for service beans, Service beans are dependent on DAO beans, DAO beans are dependent on service beans, Service beans are depends on controller classes.

What are the dependencies in Spring application ?

In an object oriented system we have a collection of objects. One object needs to collaborates with another object to provide service to the end users. To process one business service in an enterprise application, DAO object collaborates with service objects. Service objects collaborates with controller objects. An object in need of another object service is known as dependent. An object that provides the service is known as dependency. In enterprise application the controller object has the following responsibilities.

  1. Capturing the user input.
  2. Calling the business method of the service object.
  3. Getting processed data and making it available to the view components (mostly JSP).

Note: Here controller object is dependent and service object is dependency

Service object has the following responsibilities:

  1. Receiving the request from controller object.
  2. Requesting DAO object to get database data.
  3. Processing data by applying business rules and returning processed data to the controller object.

NOTE: Here service object is dependent and DAO object is dependency.

DAO object uses any data accessing mechanism to get data from database and gives to Service object.


spring example


Share Button

Spring web MVC module



Spring web MVC module is a part of spring software I.e. used to build presentation layer for Java based enterprise application. By using spring MVC module we can develop business application. To build presentation layer we can use the following things:

  1. Java language for coding
  2. Servlets and JSP as web technology.
  3. Few design patterns
  4. Web application framework like spring, struts, JSF etc

Direct using servlets and JSP in building the presentation layer is time consuming also this logic is not reusable. To address these problems Spring Web-MVC module is used instead of servlets and JSP.

what is AOP module ?

It is a part of spring software. Aspect oriented programming (AOP) module is used to build service layer. Every service layer comprises of two kinds of modules. 1. Primary Concerns 2. Secondary Concerns or cross cutting concerns.

What is spring beans ?

It is a java class that runs within the confines of the spring container is known as spring bean. A spring bean is a POJO class I.e. spring bean don’t implement any spring framework related interface or extended a spring framework or spring API related class. Even though motivation for spring bean is a Java bean, a spring bean doesn’t follow Java bean specification.

What is spring container ?

A pure Java application that control spring bean is known as spring container. A spring container is not a J2EE container unlike web container and EJB container. Spring container is light weight container in which all the core functionality provided by spring framework is available through spring container. Spring container provides following basic services.

  1. Life cycle management of spring bean without any component contract unlike Servlets and EJB.
  2. Configuration of spring bean I.e. spring container makes use of an external file to change the state and behavior of spring bean. Without the need of bean source code getting modified.


Share Button

Contribution of Struts in WEB application development



Struts provide reusable flow control logic to the maximum possible extents for any kind of web application. It provides validation support, capturing user input. Struts framework doesn’t provide any code for presentation layer.

struts architecture

Architectural elements are as follows.

  1. ActionServlet: It belongs to controller layer. It is built in class given by struts framework, it is simple HttpServlet. Struts developer should also map every request to be passed via ActionServlet.
  2. FormBeans: It is used for view layer. FormBeans are also known as Action form
  3. Action Classes: It belongs to controller layer. In a struts application for each task we have to defined a user defined class that extends given Action class. An Action class perform the following duties.

1. Capturing user input from the form bean.

2. Invoking the business method of model layer.

3. Providing view information to the Action Servlet.

4. JSP’s: It belongs to view layer.

5. Struts-config.xml: It is used to establish relationship between controller and view layer, it doesn’t belongs to any layer

Flow control in struts application

Untitled Diagram

Ex. Schematic representation of a struts application in which end user entered account number into the web page and expecting account details.

Accounting Application: AccountInput.jsp, AccountFound.jsp, AccountNotFound.jsp, Exception.jsp,,, Struts-Config.xml, web.xml


System requirements to develop and execute struts application:

  1. Install J2EE platform (jdk 1.6 or above)
  2. Install Tomcat 6.x.x or above.
  3. Install Struts 1.x and above.
  4. Install eclipse or NetBean IDE.

Steps to develop a struts application

  1. Creating structured hierarchy of directive.
  2. Developing model layer as knowing all properties of all the business method of service layer.
  3. Developing all the JSP of the application.
  4. Struts-config.XML development.


Share Button

Spring framework tutorial



Spring framework is considered as light weight application framework by taking the following things into consideration

  1. Size of the container
  2. Processing overhead involved
  3. Testability of application components
  4. POJO programming model

What is the architecture of Spring framework ?


Spring software has the following components

  1. Core Module
  2. AOP (Aspect Oriented Programming)
  3. JDBC module
  4. ORM (Object Relational Mapping)
  5. Enterprise Module
  6. Web MVC module

What are the POJO programming model ?

A POJO class does not implement or does not extends any framework specific Java class or interface. A POJO class is nothing but plain old Java object class. A non POJO class has the following limitations

  1. Testability of class become complex
  2. Processing overhead will be excessive.

To overcome above limitations POJO class are advisable in building any layer of application particularly service layer.

What is Spring JDBC framework and DAO model ?

For any Java project to build data access layer JDBC technology is required. If we use JDBC technology we are responsible for following things.

  1. Resource Allocation
  2. Resources Releasing
  3. Exception Handling
  4. Data Accessing

If we used spring DAO model, it takes the responsibility of first three responsibility. Developer is only responsible for data accessing.

What is ORM module ?

Spring DAO model has some limitations even though it eliminate the complexity involved in the direct access of JDBC therefore for all spring project we can’t use DAO module to build the data access layer. For new approach to interact with database whether only JDBC is insufficient, so in this situation we can develop application using Object Relational Mapping (ORM) technique. To implement ORM so many software are available in market e.g. hibernate, toplink etc.



Share Button

Introduction to Spring Framework


What is Spring framework ?

Spring is neither J2EE nor J2SE but uses both. Spring is an open source software that can be used in all layer of Java based enterprise application. For reducing the complexity of application we can use Spring framework. Spring is basically develop for business services development. Ex.

  1. Funds transfer
  2. utility bill payment
  3. issuing a demand draft
  4. issuing a policy

Any computer application I.e. developed to computerized business services offered by the business organization is known as business application.

Architecture of Java based enterprise application

Enterprise application using no framework.


Enterprise application using popular framework and Java technologies


Enterprise application with Spring framework and Java technologies


Any industry standard Java based enterprise application is logically partitioned into four layers depending upon the role it placed in application.

  1. Presentation layer
  2. Service layer (business layer)
  3. Data access layer
  4. Data layer

1.Presentation layer: It is a set of Java class responsible for generating user input screen and response pages is known as presentation layer. It is also known as User interface layer.

2.Service Layer: Programmatic implementation of business rules of an enterprise is nothing but business layer. To develop service layer for enterprise application three things are used in industry.

  1. Spring
  2. EJB
  3. Web Services

3. Data access layer: It is set of Java classes whose methods are exclusively used for performing CRUD operations with a relational database. In data access layer no business logic should be implemented.

4.Data layer: in almost all enterprise application application a relational database management system is used it is called as data layer. ex: Oracle, MySql, MS SQL Server, Sybase, DB2 etc.

When one layer is communicate with another layer tight coupling should not promoted. Integration must be loosely coupled means one layer technology should not depends on another layer.

integration of layers

How is Spring evolved ?

In early days of Java based enterprise application used Java Bean to build business logic or service layer. Java Bean are simple to deals with, but they lacked enterprise capabilities like transaction management, security and distributed capabilities which are mandatory for business logic.


Share Button

Model View Controller (MVC) Architecture



What is MVC in connection with a web application ?

Any industry standard web application I.e. real time website uses three logic

  1. application logic
  2. Presentation logic
  3. Businesses logic

These three logic must be separated to get various benefit and to overcome various problems. Model, View & Controller is a solution for dividing these three logic. MVC is a design pattern that promote loose coupling between three concerns. It provides the following benefit

  1. Without effecting one concern with another we can develop application.
  2. Parallel development
  3. Re usability of code

According to MVC design pattern application logic in placed in controller, Presentation logic is view, and business logic in model. Representation of MVC in a real time web based enterprise application.

Controller receiving the client request, capturing the user input and performing server side validation for the user input.

Case 1: Validation failed controller identify appropriate input page and switch the control to that page.

Case 2: Validation Successful controller identifying the appropriate business component and it’s appropriate business method for data processing. Business method is executed and data is processed according to business rules of organization. Currently in industry the business method is belongs to either Spring Bean or EJB. Business method return the result to the controller.



Case 1: Processing is successful and the processed data is return to the controller.

Case 2: Requested data is not available and hence exception is generated to the controller.

Case 3: While processing data some abnormal events occur and this are propagated to the controller.

What is the drawbacks if we develop a web application without a framework ?

In a MVC based application, controller logic used for web application action is almost similar and difficult, if we develop application without framework then we need to develop controller for every application. A controller developed for one application can’t be reused for other application of same domain. If we develop controller without framework it is very time consuming job.


Share Button