Spring Framework - Notes Prepared

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Spring Framework:

Spring is a lightweight framework


It can be thought of as a framework of frameworks because it provides support
to various frameworks such as Struts, Hibernate, Tapestry, EJB, JSF etc

Spring Framework Modules:

The Spring framework comprises several modules such as IOC, AOP, DAO,
Context, ORM, WEB MVC etc.

Inversion Of Control (IOC) and Dependency Injection

These are the design patterns that are used to remove dependency from the
programming code
They make the code easier to test and maintain. Let's understand this with the
following code:

class Employee{
Address address;
Employee(){
address=new Address();
}
}
In such case, there is dependency between the Employee and Address (tight
coupling). In the Inversion of Control scenario, we do this something like this:

class Employee{
Address address;
Employee(Address address){
this.address=address;
}
}
Thus, IOC makes the code loosely coupled. In such case, there is no need to
modify the code if our logic is moved to new environment
Advantages of Spring Framework

There are many advantages of Spring Framework.

Predefined Templates: (SF provides template for JDBC, Hibernate, JPA etc.)

Loose Coupling: (Spring applications are loosely coupled because of dependency


injection)

Lightweight: (Spring framework is lightweight because of its POJO


implementation)

Easy to test: (The Dependency Injection makes easier to test the application. The
EJB or Struts application require server to run the application but Spring
framework doesn't require server)

Fast Development: (The Dependency Injection feature of Spring Framework and


it support to various frameworks makes the easy development of JavaEE
application.)

Powerful abstraction: (It provides powerful abstraction to JavaEE specifications


such as JMS, JDBC, JPA and JTA.)

Declarative support: (It provides declarative support for caching, validation,


transactions and formatting)

IoC Container:

The IoC container is responsible to instantiate, configure and assemble the


objects.

The IoC container gets informations from the XML file and works accordingly

The main tasks performed by IoC container are:

to instantiate the application class


to configure the object
to assemble the dependencies between the objects

There are two types of IoC containers. They are:

1. BeanFactory
2. ApplicationContext
Both are interface act as the IOC container.

The ApplicationContext interface is built on top of the BeanFactory interface.


It adds some extra functionality than BeanFactory such as simple integration with
Spring's AOP, message resource handling (for I18N), event propagation, application
layer specific context (e.g. WebApplicationContext) for web application. So it is
better to use ApplicationContext than BeanFactory.

Bean Factory:

Resource resource=new ClassPathResource("applicationContext.xml");


BeanFactory factory=new XmlBeanFactory(resource);

Using ApplicationContext:

ApplicationContext context = new


ClassPathXmlApplicationContext("applicationContext.xml");

Dependency Injection in Spring


Dependency Injection (DI) is a design pattern that removes the dependency from
the programming code so that it can be easy to manage and test the application.

Dependency Injection makes our programming code loosely coupled. To understand


the DI better, Let's understand the Dependency Lookup (DL) first:
Spring Modules:

You might also like