Spring dependency Injection/IOC Flashcards
What is spring IOC container?
- Creates the objects
- Wire them together
- Configure them
- Manage their complete lifecycle from creation till destruction
- Uses dependency injection (DI) to manage the components that make up an application
What is dependency injection?
- We don’t create objects but describe how they should be created
- We don’t directly connect components and services together in code but describe which services are needed by which components in a configuration file
- A container ( the IOC container) is then responsible for hooking it all up
In how many ways dependency injection can be done?
There are three types of dependency injection:
- constructor injection: dependencies are provided as constructors parameters
- setters injection: dependences are assigned through java beans properties (setter methods)
Whats is the difference between constructor injection and setter injection?
Constructor injection: Theres no partial injection, doesnt overrides the setter property, it will create new instance if any modification is done
Setter injection: there can be partial injection, overrides the constructor property if both are defined,
wront create new instance if modification is done
How many types of IOC containers are there in spring?
Bean factory & ApplicationContext
A beanFactory is like a factory class that contains a collection of beans. It instantiates the beans whenever asked for by clients.
ApplicationContext: the applicationContext interface is built on top of BeanFactory interface. It adds some extra functionaly than bean factory
Differentiate between BeanFactory and ApplicationContext
Bean factory: uses lazy initialzization
- Explicitly provided a resource object using the syntax
- doesnt support internationalization
- annotation based dependency injection is not supported
Application Context
- Uses eager/aggressive initialization
- cretes and manages resources objects on its own
- supports internationalization
- Annotation based dependency injection is supported
List some of the benefits of IoC
- it minimizes the amount of code in application
- makes application easy to test
- loose coupling is promoted with minimal effort and least intrusive mechanism
- IOC containers support eager instantiation and lazy loading of services