What is Inversion of Control (IoC)?

Suppose, class A is dependent on class B. In case of tight coupling, class A is responsible for creating an object of class B. In case of loose coupling, the framework takes the responsibility of creating and populating the dependency. The control of creating dependency shifts from the class to the framework. This is called Inversion of Control (IoC).

What are the advantages of Inversion of Control?

  • IoC minimizes the amount of code in the application since the framework is now responsible for creating and wiring the dependencies.
  • Inversion of Control leads to loose coupling where it becomes easy to change implementations of an interface.
  • It leads to better testability as mock objects can be created for the application.
  • IoC supports eager instantiation as well as lazy loading.

What is the responsibility of an IoC container?

An IoC container performs the following tasks:

  • It instantiates the application class.
  • It identifies the beans along with their dependencies and wires the dependencies.
  • It manages the lifecycle of the beans from the time they are created till the time they are destroyed.

The IoC container uses the configuration metadata, in the form of an XML file or Java annotations, which contains instructions about the objects and their dependencies.

Describe the two types of IoC containers.

The two types of IoC containers are BeanFactory and ApplicationContext. These are interfaces with various implementations that act as the IoC container.

BeanFactory provides the basic functionality of an IoC container while ApplicationContext adds extra functionality like AOP, message resource handling for internationalization, and WebApplicationContext for web applications.

Spring recommends using ApplicationContext unless the resources are limited, like, for example, on a mobile device or for applet-based applications.

Give an example of the BeanFactory implementation.

The most commonly used implementation of BeanFactory is the XmlBeanFactory class. This container reads the metadata from an XML config file to create a fully configured application.

What is ApplicationContext?

ApplicationContext is a type of IoC container. It extends the BeanFactory interface.

Similar to the BeanFactory, the ApplicationContext can load bean definitions, wire beans together, and return beans upon request. Additional features of ApplicationContext that are not part of the BeanFactory support for AOP and internationalization, publishing events, and application layer-specific contexts like WebApplicationContext.

Give examples of the ApplicationContext implementations.

Commonly used implementations of ApplicationContext are:

  • ClassPathApplicationXmlContext: reads the configuration from an XML file for standalone Java applications.
  • AnnotationConfigApplicationContext: uses annotation-based configuration for standalone Java applications.
  • AnnotationConfigWebApplicationContext and WebXmlApplicationContext: for web applications.

What is the difference between BeanFactory and ApplicationContext?

The default implementation of BeanFactory uses lazy initialization. It instantiates beans when the getBean() method is called. ApplicationContext extends the BeanFactory interface but the default implementation uses eager initialization. The beans are instantiated when the application starts. However, this behavior can be overridden.

BeanFactory does not support annotation-based dependency injection. This feature was included in ApplicationContext.

How is ApplicationContext configured in Spring?

There are multiple ways to configure application context:

  • Application context can be configured using XML. The context can be created using the ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, or GenericXmlApplicationContext class which looks for an XML file defining the configuration.

  • Annotations can also be used to automatically register classes in the application context. The @Component annotation (along with @Controller, @Service, and @Repository annotations) is used on classes, and the AnnotationConfigApplicationContext class is used to create the application context.

  • Java configuration using the @Configuration annotation on classes and @Bean annotation on methods is another way to configure the application context. The AnnotationConfigApplicationContext class is used to create the context by scanning the annotations.

What is WebApplicationContext?

WebApplicationContext interface extends ApplicationContext interface. It provides configuration for web applications.

WebApplicationContext defines request, session, and global application scopes in addition to the singleton and prototype scopes in ApplicationContext.

It has the ability to resolve themes and decide which servlet it is associated with.

What happens if the context is not closed?

Not closing the context leads to resource leaks. The close() method destroys all beans, releases the locks and closes the bean factory. Similarly, using a try-with-resources block also ensures that each resource is closed when the block exits.

What is dependency injection?

Dependency injection is a concept that states that the developer should not create objects manually in the code but specify how the objects should be created. The IoC container reads this information and instantiates the object with the required dependencies.

Dependency injection is the process of finding a bean to be autowired. If class A has a dependency on class B, then the process of identifying the dependency, creating an instance of class B, and autowiring the object of class B in class A is called dependency injection.

How is dependency injection related to inversion of control?

Inversion of control (IoC) is a general concept which can be expressed in different ways. Dependency injection is an example of IoC.

IoC concept is that the control of creating and wiring the dependencies shifts from the developer to the framework. Dependency injection is the process by which the framework identifies the dependencies of an object, finds a match, and wires the dependency in the object.

What are the types of dependency injection?

A dependency can be injected in several ways:

  • Field injection
  • Setter injection
  • Constructor injection

What is constructor injection?

In constructor injection, the IoC container creates the object by calling a constructor with a number of arguments, where each argument represents a dependency on another class.

The following code example uses a constructor to inject the Engine dependency in Vehicle class.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.