IOC and Dependency Injection - ayushmathur94/DirectQuesAns_Prep GitHub Wiki

What is IOC ?

IoC is also known as dependency injection (DI). Spring IoC Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application. It gets the information about the objects from a configuration file(XML) or Java Code or Java Annotations and Java POJO class. These objects are called Beans. Since the Controlling of Java objects and their lifecycle is not done by the developers, hence the name Inversion Of Control.

Spring IoC container classes are part of org.springframework.beans and org.springframework.context packages.
Spring IoC container provides us different ways to decouple the object dependencies.
BeanFactory is the root interface of Spring IoC container.
ApplicationContext is the child interface of BeanFactory interface.
Some of the useful child-interfaces of ApplicationContext are ConfigurableApplicationContext and WebApplicationContext.

What is Dependency Injection ?

  • Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable.
  • We can implement dependency injection pattern to move the dependency resolution from compile time to runtime.
  • Loose coupling between classes can be possible by defining interfaces for common functionality and the injector will instantiate the objects of required implementation. The task of instantiating objects is done by the container according to the configurations specified by the developer.

Benefits of Dependency Injection are:
1). Separation of concerns.
2). Boiler plate code reduction in application classes because all work to initialize dependencies is handled by the injector component.
3). Configurable components makes application easily extendable.
4). Unit testing is easy with mock objects.

Disadvantages of Dependency Injection: .
1). If overused, it can lead to maintenance issue because effect of changes are known at "runtime".

How do we implement DI in Spring Framework?

We can use Spring XML based as well as Annotation based configuration to implement DI in spring applications.
See here for detailed understanding see here

What are the different types of dependency injection ?

Constructor-based dependency injection − Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.

Setter-based dependency injection − Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

Which DI would you suggest Constructor-based or setter-based DI?

Since you can mix both, Constructor- and Setter-based DI, it is a good rule of thumb to use constructor arguments for mandatory dependencies and setters for optional dependencies. Note that the use of a @Required annotation on a setter can be used to make setters required dependencies.

⚠️ **GitHub.com Fallback** ⚠️