2. Content
• What is Spring Framework?
• Key features of Spring Framework
Spring Modules
Dependency Injection and Inversion of Control
Spring AOP
• Spring Environment Setup
• Quick Steps to Develop Spring Application
• Spring Hello World Example
• Setter Injection v/s Constructor Injection
• Spring Autowiring
3. What is Spring Framework?
• Spring is a light weight and open source framework created
by Rod Johnson in 2003
• It is a framework of frameworks
• Spring framework is said to be a non-invasive.
• Spring Framework made J2EE application development little
easier, by introducing POJO model
• Spring having so much of demand because of the following 3
reasons….
Simplicity
Testability
Loose Coupling
4. Key features of Spring Framework
Spring Modules
Test
Core Container
AOP
Aspects
Instrumentation
Data Access/ Integration
Web (MVC / Remoting)
5. Spring Core Module – Loose Coupling
Tight Coupling
When an object creates the object to be used, then it is a tight coupling
situation.
As the main object creates the object itself, this object can not be changed
from outside world easily marked it as tightly coupled objects.
When an object gets the object to be used from the outside, then it is a loose
coupling situation.
As the main object is merely using the object, this object can be changed
from the outside world easily marked it as loosely coupled objects.
Loose Coupling
6. IoC and Dependency Injection
What is Inversion of Control ?
Inversion of Control is a principle in software engineering by which the control
of objects or portions of a program is transferred to a container or framework.
It's most often used in the context of object-oriented programming.
Inversion of Control can be achieved through various mechanisms such as:
Strategy design pattern, Service Locator pattern, Factory pattern, and
Dependency Injection (DI).
7. IoC and Dependency Injection (cont.)
What is Spring IoC Container ?
• An IoC container is a common characteristic of frameworks that implement
IoC.
• In the Spring framework, the IoC container is represented by the interface
ApplicationContext.
• The Spring container is responsible for instantiating, configuring and
assembling objects known as beans, as well as managing their lifecycle.
• The Spring framework provides several implementations of the
ApplicationContext interface — ClassPathXmlApplicationContext and
FileSystemXmlApplicationContext for standalone applications, and
WebApplicationContext for web applications.
• Here's one way to manually instantiate a container:
8. IoC and Dependency Injection (cont.)
Spring IoC Container Overview
• The configuration metadata can be represented either by;
XML
Java Annotations
9. IoC and Dependency Injection (cont.)
What is Dependency Injection?
Dependency injection is a pattern through which to implement IoC, where the
control being inverted is the setting of object's dependencies.
The act of connecting objects with other objects, or “injecting” objects into
other objects, is done by an assembler rather than by the objects themselves.
Traditional Programming Using DI
10. IoC and Dependency Injection (cont.)
Types of Dependency Injection
Spring framework provides two ways to inject dependency:-
By Setter method
• In this type of injection, the spring container uses setter method in the
dependent class for injecting its dependencies (primitive values or any).
• Spring container knows whether to perform setter or constructor
injection by reading the information from an external file called spring
configuration file
• In spring config xml, we need to inform to the spring IOC container about
setter injection by using <property/>
By Constructor
• In this type of injection, spring container uses constructor of the bean
class for assigning the dependencies.
• In spring config xml, we need to inform to the spring IOC container about
constructor injection by using <constructor –arg/>
11. Spring AOP
Introduction
• 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.
• It provides the pluggable way to dynamically add the additional concern
before, after or around the actual logic.
• AOP terminologies are Join point, Advice, Pointcut, Introduction,
Target Object, Aspect, Interceptor, AOP Proxy, Weaving
12. Spring Environment Setup
Step 1 - Setup Java Development Kit (JDK)
You can download the latest version of SDK from Oracle's Java site
https://www.oracle.com/java/technologies/javase-downloads.html
Step 2 - Install Apache Common Logging API
You can download the latest version of Apache Commons Logging API from
https://commons.apache.org/logging/
Step 3 - Setup Eclipse IDE
To install Eclipse IDE, download the latest Eclipse binaries from
https://www.eclipse.org/downloads/
Step 4 - Setup Spring Framework Libraries
Download the latest version of Spring framework libraries from
https://repo.spring.io/release/org/springframework/spring
13. Quick Steps to Develop Spring Application
• Spring environment starts by loading spring configuration xml file into Resource
Interface object.
• Actually this configuration file contains all bean configurations about our
application [ something like struts.xml in Struts 2 ]
• ClassPathResource is the implementation class of the Resource interface.
• Spring IOC container is called BeanFactory and this container is responsible for
creating the bean objects and for injecting it’s dependencies throughout our
applications.
• The XmlBeanFactory is the implementation class of the BeanFactory. There are
many methods in the BeanFactory interface. One method is getBean(), which
returns the object of the associated class.
• Syntax
Resource res = new ClassPathResource(“Our Config xml file name”);
BeanFactory factory = new XmlBeanFactory(res);
14. Spring Hello World Example
Demo in Eclipse
Using Setter Injection & Constructor Injection with Primitive Values
15. Setter Injection v/s Constructor Injection
Partial dependency: can be injected using setter injection but it is not
possible by constructor. Suppose there are 3 properties in a class, having 3 arg
constructor and setters methods. In such case, if you want to pass information
for only one property, it is possible by setter method only.
Overriding: Setter injection overrides the constructor injection. If we use
both constructor and setter injection, IOC container will use the setter
injection.
Changes: We can easily change the value by setter injection. It doesn't
create a new bean instance always like constructor. So setter injection is
flexible than constructor injection.
16. Spring Autowiring
Autowiring feature of spring framework enables you to inject the object
dependency implicitly.
It internally uses setter or constructor injection.
Autowiring can't be used to inject primitive values. It works with
reference only.
Autowiring Modes
• byName
• byType
• constructor
• autoDetect
• none