Spring Intro
Spring Intro
DEVELOPMENT PROGRAM
Spring Introduction & Dependency Injection
OUTLINE
• Dependency Injection
• Examples of how you, as an application developer, can use the Spring platform advantage:
• Make a Java method execute in a database transaction without having to deal with
transaction APIs
• Make a local Java method a remote procedure without having to deal with remote
APIs.
• Make a local Java method a message handler without having to deal with JMS APIs.
SPRING FRAMEWORK
• Some Advantages of Spring framework
• Easy to test
• Modularity
fi
DEPENDENCY
• You work in an organization where you and your colleagues tend to travel
a lot. Your typical travel planning routine might look like the following:
• The new agencies, and their new contact mechanisms (say the
new agencies offer internet based services and the way to do
the bookings is over the internet instead of over the phone)
• Over the phone you simply state the destination, desired arrival date and
time by responding to a programmed set of questions
• The ight reservations are made for you, the cab gets scheduled for the
appropriate time, and the tickets get delivered to you
fl
DEPENDENCY
• When class A uses some functionality of class B, then its said that class
A has a dependency of class B.
TIGHTLY COUPLING
LOOSE COUPLING
DEPENDENCY INJECTION
• What is dependency Inject?
• Spring IoC container injects those dependencies when it creates the bean.
fi
SPRING IOC CONTAINER
• The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s
IoC container.
• The BeanFactory interface provides an advanced con guration mechanism capable of managing any type of
object:
• XML
• Annotation
fi
fi
fi
fi
XML CONFIGURATION
• The class attribute de nes the type of the bean and uses the fully quali ed
classname.
fi
fi
fi
fi
SPRING IOC CONTAINER
• Dependency Inject in Spring can be done in three
ways:
• Constructor
• Setter
• Field
CONSTRUCTOR INJECTION
• In the case of constructor-based dependency injection, the
container will invoke a constructor with arguments each
representing a dependency we want to set
CONSTRUCTOR INJECTION
• Constructor argument resolution matching occurs by using the argument’s
type
• The container can use type matching with simple types if you explicitly
specify the type of the constructor argument by using the type or index
or name attribute
fi
fi
fi
CONSTRUCTOR INJECTION
SETTER INJECTION
• Setter-based DI is accomplished by the container calling setter methods
on your beans after invoking a no-argument constructor or a no-
argument static factory method to instantiate your bean
CONSTRUTOR VS SETTER
INJECTION
SPRING BEAN SCOPE
Scope Description
fi
ANNOTATION BASED
CONFIGURATION
• How to do Annotation Based Con guration
• Match by Type
• Match by Quali er
• Match by Name
fi
ANNOTATION DI
• @Bean
• @Con guration
• @ComponentScan
fi
fi
fi
BEAN LIFE CYCLE
LIFE CYCLE CALLBACKS
• Spring bean factory controls the creation and
destruction of beans. To execute some custom
code, it provides the call back methods which can
be categorized broadly in two groups