Spring
It was developed by Rod Johnson in 2003. Spring framework makes the easy
development of JavaEE application.
It is helpful for beginners and experienced persons.
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. The framework, in broader sense, 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. We will learn these modules in next page. Let's understand the
IOC and Dependency Injection first.
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:
1. class Employee{
2. Address address;
3. Employee(){
4. address=new Address();
5. }
6. }
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:
1. class Employee{
2. Address address;
3. Employee(Address address){
4. [Link]=address;
5. }
6. }
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.
In Spring framework, IOC container is responsible to inject the dependency. We
provide metadata to the IOC container either by XML file or annotation.
Advantage of Dependency Injection
ADVERTISEMENT
ADVERTISEMENT
o makes the code loosely coupled so easy to maintain
o makes the code easy to test
Advantages of Spring Framework
There are many advantages of Spring Framework. They are as follows:
1) Predefined Templates
Spring framework provides 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.
ADVERTISEMENT
ADVERTISEMENT
Let's take the example of JdbcTemplate, you don't need to write the code for exception
handling, creating connection, creating statement, committing transaction, closing
connection etc. You need to write the code of executing query only. Thus, it save a lot
of JDBC code.
2) Loose Coupling
The Spring applications are loosely coupled because of dependency injection.
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.
4) Lightweight
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. That is why it is said non-invasive.
5) Fast Development
The Dependency Injection feature of Spring Framework and it support to various
frameworks makes the easy development of JavaEE application.
6) Powerful abstraction
It provides powerful abstraction to JavaEE specifications such as JMS, JDBC, JPA and
JTA.
7) Declarative support
It provides declarative support for caching, validation, transactions and formatting.
IoC Container
1. IoC Container
2. Using BeanFactory
3. Using ApplicationContext
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:
ADVERTISEMENT
ADVERTISEMENT
o to instantiate the application class
o to configure the object
o to assemble the dependencies between the objects
There are two types of IoC containers. They are:
1. BeanFactory
2. ApplicationContext
Difference between BeanFactory and the ApplicationContext
The [Link] and the
[Link] interfaces acts 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.
Using BeanFactory
The XmlBeanFactory is the implementation class for the BeanFactory interface. To use
the BeanFactory, we need to create the instance of XmlBeanFactory class as given
below:
1. Resource resource=new ClassPathResource("[Link]");
2. BeanFactory factory=new XmlBeanFactory(resource);
The constructor of XmlBeanFactory class receives the Resource object so we need to
pass the resource object to create the object of BeanFactory.
Using ApplicationContext
The ClassPathXmlApplicationContext class is the implementation class of
ApplicationContext interface. We need to instantiate the
ClassPathXmlApplicationContext class to use the ApplicationContext as given below:
1. ApplicationContext context =
2. new ClassPathXmlApplicationContext("[Link]");
The constructor of ClassPathXmlApplicationContext class receives string, so we can
pass the name of the xml file to create the instance of ApplicationContext.
Spring AOP
Aspect Oriented Programming (AOP) compliments OOPs in the sense that it also
provides modularity. But the key unit of modularity is aspect than class.
AOP breaks the program logic into distinct parts (called concerns). It is used to increase
modularity by cross-cutting concerns.
A cross-cutting concern is a concern that can affect the whole application and should
be centralized in one location in code as possible, such as transaction management,
authentication, logging, security etc.
Why use AOP?
It provides the pluggable way to dynamically add the additional concern before, after
or around the actual logic. Suppose there are 10 methods in a class as given below:
1. class A{
2. public void m1(){...}
3. public void m2(){...}
4. public void m3(){...}
5. public void m4(){...}
6. public void m5(){...}
7. public void n1(){...}
8. public void n2(){...}
9. public void p1(){...}
10.public void p2(){...}
11. public void p3(){...}
12. }
There are 5 methods that starts from m, 2 methods that starts from n and 3 methods
that starts from p.
Understanding Scenario I have to maintain log and send notification after calling
methods that starts from m.
Problem without AOP We can call methods (that maintains log and sends notification)
from the methods starting with m. In such scenario, we need to write the code in all the
5 methods.
But, if client says in future, I don't have to send notification, you need to change all the
methods. It leads to the maintenance problem.
Solution with AOP We don't have to call methods from the method. Now we can
define the additional concern like maintaining log, sending notification etc. in the
method of a class. Its entry is given in the xml file.
In future, if client says to remove the notifier functionality, we need to change only in
the xml file. So, maintenance is easy in AOP.
ADVERTISEMENT
Where use AOP?
AOP is mostly used in following cases:
ADVERTISEMENT
o to provide declarative enterprise services such as declarative transaction
management.
o It allows users to implement custom aspects.
AOP Concepts and Terminology
AOP concepts and terminologies are as follows:
o Join point
o Advice
o Pointcut
o Introduction
o Target Object
o Aspect
o Interceptor
o AOP Proxy
o Weaving
Join point
Join point is any point in your program such as method execution, exception handling,
field access etc. Spring supports only method execution join point.
Advice
Advice represents an action taken by an aspect at a particular join point. There are
different types of advices:
o Before Advice: it executes before a join point.
o After Returning Advice: it executes after a joint point completes normally.
o After Throwing Advice: it executes if method exits by throwing an exception.
o After (finally) Advice: it executes after a join point regardless of join point exit
whether normally or exceptional return.
o Around Advice: It executes before and after a join point.
Pointcut
It is an expression language of AOP that matches join points.
Introduction
It means introduction of additional method and fields for a type. It allows you to
introduce new interface to any advised object.
Target Object
It is the object i.e. being advised by one or more aspects. It is also known as proxied
object in spring because Spring AOP is implemented using runtime proxies.
Aspect
It is a class that contains advices, joinpoints etc.
Interceptor
It is an aspect that contains only one advice.
AOP Proxy
It is used to implement aspect contracts, created by AOP framework. It will be a JDK
dynamic proxy or CGLIB proxy in spring framework.
ADVERTISEMENT
ADVERTISEMENT
Weaving
It is the process of linking aspect with other application types or objects to create an
advised object. Weaving can be done at compile time, load time or runtime. Spring
AOP performs weaving at runtime.
AOP Implementations
AOP implementations are provided by:
1. AspectJ
2. Spring AOP
3. JBoss AOP
Spring AOP
Spring AOP can be used by 3 ways given below. But the widely used approach is
Spring AspectJ Annotation Style. The 3 ways to use spring AOP are given below:
1. By Spring1.2 Old style (dtd based) (also supported in Spring3)
2. By AspectJ annotation-style
3. By Spring XML configuration-style(schema based)