This file contains the Spring Framework introduction.
Mainly about what is Spring Framework and its components, feature, advantages with a simple program example.
2. What is Spring ?
1. Spring Framework is a open source Java Platform that provides
comprehensive infrastructure support for developing robust Java
application very easily and rapidly.
2. Spring framework was developed by Rod Johnson and was first
released in 2003 in Spring Organization now Pivotal.
3. Earlier name of Spring Framework was Interface21 later changed to
Spring based on Spring season.
4. Spring Framework is replacement of EJB’s (Enterprise Java Beans)
because of heavy weight of EJB’s
3. Why Spring ?
1. Spring is the most popular application development framework for
enterprise Java. Millions of developers around the world use Spring
Framework to create high performing, easily testable, and reusable
code.
2. Spring is lightweight when it comes to size and transparency. The
basic version of Spring framework is around 2MB.
3. The core features of the Spring Framework can be used in
developing any Java application, but there are extensions for
building web applications on top of the Java EE platform.
4. Spring framework targets to make J2EE development easier to use
and promotes good programming practices by enabling a
POJO-based programming model.
4. Advantages of Spring
1. Lightweight - Spring framework is lightweight because of its POJO
implementation.The Spring framework doesn’t force any
programmer to inherit any class or implements any interface. That
is why it is said non-invasive, Enable you to write powerful, scalable
applications using POJO.
2. Easy to develop J2EE application - The Dependency Injection
feature of Spring Framework and it support to various frameworks
makes the easy development of JavaEE application.
3. 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.
5. 1. Loose Coupling - The Spring applications are loosely coupled
because of dependency injection and handles injecting dependent
components without a component knowing where they came from
(IoC).
2. Powerful abstraction - It provides powerful abstraction to JavaEE
specifications such as JDBC.
3. Declarative support - It provides declarative support for caching,
validation, transactions and formatting.
4. Portable - can use server - side in web/ejb app, client-side in swing
app, business logic is completely portable.
5. Lifecycle - Responsible for managing all your application
components, particularly those in the middle tier container sees
components through well-defined lifecycle: init(), destroy()
6. Modules of the Spring Framework
1. Core - Inversion of Control (IoC) and Dependency Injection.
2. AOP - These modules support aspect oriented programming
implementation where you can use Advices, Pointcuts etc. to
decouple the code.
3. DAO - Data Access Object support, transaction management,
JDBC-abstraction
4. ORM - Object Relational Mapping data access, integration layers
for Hibernate
5. MVC - Model-View-Controller implementation for web applications
6. Expression Language - It is an extension to the EL defined in JSP. It
provides support to setting and getting property values, method
invocation, accessing collections and indexers, named variables.
7. IOC and Dependency Injection
1. Dependency Injection is the main functionality provided by Spring
IOC (Inversion of Control).
2. The Spring-Core module is responsible for injecting dependencies
through either Constructor or Setter methods in POJO class.
3. Dependency Injection in Spring also ensures loose-coupling
between the classes.
Need for Dependency Injection:
1. Spring IOC resolves dependencies with Dependency Injection,
which makes the code easier to test and reuse.
2. Loose coupling between classes can be possible by defining
interfaces for common functionality and the injector will instantiate
the objects of required implementation.
8. Example
Tightly Coupling - (without IOC)
class Employee{
Address address;
Employee()
{address=new Address(“xyz street”, ”Lucknow”, ”UP”);
}}
One class object is created in another class in tight coupling.Leads to
a.Stop server
b.Recomplie
c.Create jar and war file
d.Redeploy
e.Restart the server.
9. Loose Coupling
1. with IOC (Constructor Injection)
class Employee{
Address address;
Employee(Address address)
{this.address=address;} }
2. with IOC (Setter Injection)
class Employee{
Address address;
public void setAddress(Address address)
{this.address=address;} }
No need to stop the existing component, in real time it is better.
11. 1. Spring allows to decouple software layers by injecting a
component’s dependencies at runtime rather than having them
declared at compile time via importing and instantiating classes.
2. Spring provides integration for J2EE services such as EJB, JDBC,
JNDI, JMS, JTA. It also integrates several popular ORM toolkits
such as Hibernate and JDO and assorted other services as well.
3. One of the highly touted
4. Spring is built on the principle of unchecked exception handling.
This also reduces code dependencies between layers. Spring
provides a granular exception hierarchy for data access operations
and maps JDBC, EJB, and ORM exceptions to Spring exceptions
so that applications can get better information about the error
condition.
13. How to Start Using Spring
1. create the class
2. create the xml file to provide the values
3. create the test class
4. Load the spring jar files
5. Run the test class
17. 4. Load the jar files required for spring framework
There are mainly three jar files required to run this application.
1. org.springframework.core-3.0.1.RELEASE-A
2. com.springsource.org.apache.commons.logging-1.1.1
3. org.springframework.beans-3.0.1.RELEASE-A
5. Run the TEST FILE
18. Containers in Spring
1. Core Container (slower)
a. It is called Lazy Container.
b. It create object on demand one by one.
c. It will create the object when the bean file is loaded.
ClassPathResource res=new ClassPathResource(“Spring.xml”);
XmlBeanFactory fact=new XmlBeanFactory(res);
fact.getBean(“abc”);
2. J2EE Container (faster)
a. It is called Eager Container.
b. It create all object at once.
c. It cerate the object as soon as object of ApplicationContext is made.
ApplicationContext con=new
ClassPathXmlApplicationContext(“Spring.xml”);
19. Bean File
• Typical java bean with a unique id
• In spring there are basically two types
1. Singleton
One instance of the bean created and referenced each time it
is requested
<bean class=“bean.Test” id=“t” scope=“singleton”>
One object for all caller.
2. Prototype (non-singleton)
<bean class=“bean.Test” id=“t” scope=“prototype”>
multiple object i.e. one one object for each caller.