SPRING FRAMEWORK
• Spring is a lightweight , open source , java based framework which
makes application development easy and fast.
• It is developed by Rod Johnson in 2003.
• It is used to develop enterprise level applications.
• It is known as a framework of frameworks because it provides
support to various other frameworks such as Hibernate, JPA etc.
• The spring framework, in other words can be defined as a structure
where we find solution of the various technical problems.
• The Spring framework comprises several modules such as IOC, AOP,
DAO, Context, ORM, WEB MVC etc.
Advantages of Spring Framework
• Spring framework provides predefined templates for JDBC, Hibernate, JPA
etc. technologies. So there is no need to write too much code. It hides the
basic steps of these technologies.
• The Spring applications are loosely coupled because of dependency
injection.
• The Dependency Injection in spring framework makes it easier to test the
applications.
• Spring framework is lightweight because of its POJO implementation. The
Spring Framework doesn't force the programmer to inherit any class or
implement any interface.
• The Dependency Injection feature of Spring Framework and it support to
various frameworks makes the easy development of JavaEE application.
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.
• Spring IOC container is the core of spring framework. It is made up of core
container and J2EE container.
• The IOC container uses Dependency Injection(DI) to manage the components.
• It creates the objects , configures, assembles their dependencies and
manages their entire life cycle.
• The objects created and managed by IOC container are known as spring
beans.
• Since the controlling of objects is not by programmer , instead done by
spring container , it is called as inversion of control.
• The main features of spring IOC container are :
1. Creating object .
2. managing the object dependencies
3. Helps the application to be configurable.
• IOC containers are of two types :
1. BeanFactory (Core container)
2. ApplicationContext (J2EE container).
• BeanFactory and ApplicationContext are both interfaces
which acts as IOC containers.
BeanFactory interface :
It is the most basic version of IOC container providing a configuration
mechanism to instantiate , configure and manage the life cycle of objects.
The BeanFactory loads the bean definitions and their dependencies
based on xml file only. It doesn’t support annotations based
configuration.
AnnotationContext interface :
It is the sub interface of BeanFactory interface. Hence , it supports all the
features of BeanFactory interface.
It is the advanced version of IOC container providing a easy
configuration mechanism to instantiate , configure and manage the life
cycle of objects.
It supports xml based configurations as well as supports annotation
based configuration.
Information about object creation can be provided to IOC
container in two ways :
1. Using XML file
2. Using annotations
Steps to create a Spring core project :
1. Create a maven project and add spring context dependency.
2. Create a POJO class (class for which you want spring IOC to create object).
3. Create an xml file and configure the bean.
(xml file should be created directly inside src/main/java folder only.)
(xml file can have any name).
Configure.xml
• Create the application class to access the bean by creating IOC container
(ApplicationContext) using its implementing classes such as
ClassPathXmlApplicatonContext OR AnnotationConfigApplicationContext.