Spring dependency Injection/IOC Flashcards

1
Q

What is spring IOC container?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is dependency injection?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In how many ways dependency injection can be done?

A

There are three types of dependency injection:

  1. constructor injection: dependencies are provided as constructors parameters
  2. setters injection: dependences are assigned through java beans properties (setter methods)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Whats is the difference between constructor injection and setter injection?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many types of IOC containers are there in spring?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Differentiate between BeanFactory and ApplicationContext

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

List some of the benefits of IoC

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly