Spring IoC
Container
Ramesh
RameshFadatare
Fadatare((Java
JavaGuides)
Guides)
Tight Coupling
Car
Client Traveler Bike
Bicycle
Ramesh Fadatare ( Java Guides)
Loose Coupling
Car
Client Traveler Vehicle Bike
Bicycle
Ramesh Fadatare ( Java Guides)
Spring IoC Container Example
1. Tight Couplin
2. Loose Coupling using Interface
3. Using Spring IoC Container to create and
manage objects using Java based con guration
Ramesh Fadatare ( Java Guides)
g
fi
Creating Objects Manually
JVM
Car Bike Traveler
Ramesh Fadatare ( Java Guides)
Spring IOC Container
JVM
Spring Container
Bike
Car
Traveler
Cycle
Ramesh Fadatare ( Java Guides)
Spring IoC Container
1. Responsible for creating the objects (Spring beans
2. Responsible for injecting one abject into another object (using DI
3. Managing the bean’s entire life-cycle - It manages the complete lifecycle of
an bean from creation to destructio
4. Container uses the information provided in the con guration le (or
annotations) to create the objects, and it uses dependency injection to supply
the objects with their dependencies.
5. The con guration metadata is represented in XML, Java annotations, or Java
code
6. By using the IoC container, the objects are more loosely coupled and easier
to test, since their dependencies can be easily swapped out with mock
objects.
Ramesh Fadatare ( Java Guides)
.
fi
n
fi
fi
)
Spring IoC Container
Ramesh Fadatare ( Java Guides)
Spring IoC Container Types
1. BeanFactory container
2. ApplicationContext container
Ramesh Fadatare ( Java Guides)
BeanFactory Container
1. Responsible for creating the beans, con guring
the beans and managing the bean’s entire life-
cycle.
Ramesh Fadatare ( Java Guides)
fi
ApplicationContext Container
1. Responsible for creating the beans, con guring the beans and
managing the bean’s entire life-cycle
2. Enterprise Application Features
• It provides messaging (i18n or internationalization) functionalit
• Event publication functionalit
• Annotation-based dependency injectio
• Easy integration with Spring AOP features
• Supports almost all types of bean scopes
Ramesh Fadatare ( Java Guides)
y
fi
@Primary Annotation
We use @Primary annotation to give higher
preference to a bean when there are multiple beans
of the same type.
Ramesh Fadatare ( Java Guides)
Steps for Java based configuration
1. Create Con guration class with @Con guration
annotatio
2. Create method and annotated it with @Bean
annotatio
3. Create Spring IoC Container (ApplicationContext)
and Retrieve Spring bean from Spring IoC
container.
Ramesh Fadatare ( Java Guides)
n
fi
fi
1. Create Configuration class with @Configuration annotation
Ramesh Fadatare ( Java Guides)
2. Create method and annotated it with @Bean annotation
Ramesh Fadatare ( Java Guides)
3. Create Spring IoC container and retrieve bean
Ramesh Fadatare ( Java Guides)