The document discusses various aspects of dependency injection (DI) in Spring, including:
- Using constructor and setter injection to inject dependencies into beans
- Declaring inner beans and aliases
- Initializing collections in beans
- Understanding different bean scopes like singleton and prototype
- Autowiring bean dependencies
- Making beans aware of their names and the application context
- Inheriting properties from parent bean definitions
- Lifecycle callbacks for initializing and destroying beans
- Method injection and lookup method injection
The document provides examples and explanations of these different DI features in Spring. It describes how Spring manages object dependencies and allows the container to inject collaborating objects.
7. DI ?
• DI is how objects or bean using Spring are brought together by container
to accomplished the task
• It is AKA of IoC
• In most application and object is responsible for its own dependencies or
associated objects and this is mostly achieved using JNDI
• In DI approach we allow container to manages dependencies of object
creation and association
Hollywood ; don’t call me I will call you
rgupta.trainer@gmail.com
9. • Any object outside
container get bean by
providing ref
• We know object requiring
bean contact to container ie
Application context that
refer SpringXML and create
a Spring managed bean and
that can be referred by
requesting object
outside/inside the
container
rgupta.trainer@gmail.com
15. Inner Beans
• Remove the definition of
Point bean and put into
triangle bean
• Remove the ref tag and put
definition inside.
• No performance adv.
• whenever a bean is used for
only one particular property,
it’s advise to declare it as an
inner bean
rgupta.trainer@gmail.com
16. Aliases
• alias giving name
to same bean
• Use alias tag
• Now bean can be
referred by new
name
rgupta.trainer@gmail.com
17. Spring bean scopes
• In Spring, bean scope is used to decide which
type of bean instance should be return from
Spring container back to the caller.
• In most cases, you may only deal with the
Spring’s core scope – singleton and prototype,
and the default scope is singleton.
rgupta.trainer@gmail.com
18. Bean Scopes
Scope Description
Singleton (Default)Only one single instance will be
created
Prototype Creates any number of instances from a
single bean configuration
Request Scope of the bean instance will be limited to
the Request life cycle
Session Limited to session
Global session Limited to global session- Portlet context.
<bean name =“student” class =“Student” scope =“prototype”/>
rgupta.trainer@gmail.com
23. Spring Collection
• What if member variable is collection
major collection types are supported in
Spring
List – <list/>
Set – <set/>
Map – <map/>
Properties – <props/>
rgupta.trainer@gmail.com
26. Auto Wiring
• Skip some of the configuration that we have
to do by intelligent guessing what ref is
• AKA shortcut.
• Type of auto wiring
– byName
– byType (for only one composite object)
– constructor
rgupta.trainer@gmail.com
28. Aware interfaces..
• BeanName Aware interface
• Want to know the name of bean configured
• ApplicationContextAware
• Getting Application context
• We need to implements ApplicationContextAware
Container itself call setter related to aware interface
before creation of any bean.
rgupta.trainer@gmail.com
29. Bean Definition Inheritance
• Lets say we have 100 bean definition in xml ,
lets we have common definition in each bean
definition aka template
rgupta.trainer@gmail.com
31. Lifecycle Callbacks
• Spring provide us call-back method for life cycle
of bean
• for initialization of bean
• for cleanup of bean
EX: Shut down hook
• closing app context for SE applications
• use class AbstractAppicationContext, when main finished
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("springWithAutoWiring.xml");
ctx.registerShutdownHook();
rgupta.trainer@gmail.com
32. cofig init and destroyed method
• Choice I
• implements InitializingBean,DisposableBean
• Choice II
• Dont want to bind to spring interface interfaces......
<bean id="tringle" class="com.ex3.code.Tringle" autowire="byName" init-method="myInit" destroy-
method="myDestroy">
What happens if both are there?
first spring then my method is called.
rgupta.trainer@gmail.com
33. public class Tringle implements
public void setPointC(Point pointC) {
InitializingBean,DisposableBean{ this.pointC = pointC;
}
private Point pointA; public void draw() {
private Point pointB; System.out.println(pointA);
System.out.println(pointB);
private Point pointC; System.out.println(pointC);
}
public Point getPointA() {
@Override
return pointA; public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
} System.out.println("called after init of bean");
public void setPointA(Point pointA) { }
@Override
this.pointA = pointA; public void destroy() throws Exception {
System.out.println("called after destruction of bean");
} }
public Point getPointB() {
}
return pointB; }
}
public void setPointB(Point pointB) {
this.pointB = pointB;
}
public Point getPointC() {
return pointC;
}
rgupta.trainer@gmail.com
34. Method Injection – Method Replace
class MobileStore{
public String buyMobile(){
return "Bought a Mobile Phone";
}}
class MobileStoreReplacer implements MethodReplacer{
public Object reimplement(Object obj, Method method, Object[] args)
throws Throwable{
return “Bought an iPhone”;
}
}
<bean id =“mobileStore” class =“MobileStore”>
<replace-method name =“buyMobile” replacer =“mobileStoreReplacer”/>
</bean>
<bean id =“mobileStoreReplacer” class =“MobileStoreReplacer”/>
rgupta.trainer@gmail.com
35. Lookup Method Injection
public abstract class BookStore { public interface Book {
public String bookTitle();
public abstract Book orderBook(); }
}
Managed by Spring
public class StoryBook implements Book{ public class ProgrammingBook
public String bookTitle() { implements Book{
return "HarryPotter“; } public String bookTitle() {
} return "spring programming“; }
}
• The ability of the container to override methods on
container managed beans, to return the lookup
result for another named bean in the container.
rgupta.trainer@gmail.com