Spring Interview Questions

What is a Spring Bean?

Spring Bean is an object that is instantiated, assembled, managed by Spring Dependency Injection Framework.

What is IOC or Inversion of Control in Spring?

Inversion of Control is a general design principle of creating reusable modular frameworks that are easy to maintain.
Inversion Of Control in spring means giving control of creating and instantiating the spring beans to the IOC Container.

What is Dependency Injection?

Dependency injection is a design pattern which tells how components get hold of their dependency in other words Spring container is responsible for creating the bean instances and other associate dependencies.
Dependency Injection is a pattern through which inversion of control is injected.
Dependency injection helps us achieving loose coupling.
There are 2 types of dependency injection in spring they are Constructor Injection and Setter Injection.

Difference between Bean Factory and Application Context

In IOC container spring keeps all its objects. We have 2 types of IOC container in Spring, they are Bean Factory and Application Context. The difference between them is as below.

BeanFactory ApplicationContext
BeanFactory is the root interface for accessing Container. ApplicationContext is the central interface in a spring application that provides configuration information.
BeanFactory mostly gets implemented by XmlBeanFactory This extends BeanFactory.
This is lazy loading, So bean instantiates first. This is eager loading, so bean instantiation happens before.

In an enterprise application, we prefer ApplicationContext as it has all properties of BeanFactory as well, but the only disadvantage is ApplicationContext is heavier.

What is Bean scope and how many types of scopes are available in Spring

Bean Scope is the scope of an object created from different beans. We have five types of scope in Spring. They are as below:

Scope Description
Singleton Scopes a single bean definition to a single object instance per Spring IoC container.
prototype Scopes a single bean definition to multiple object instances.
request This is valid in MVC or Rest scenarios. Scopes a single bean definition to the lifecycle of a single HTTP request;
session This is also valid in Spring MVC or Rest scenarios. Scopes a single bean definition to the lifecycle of an HTTP Session.
global session This is also valid in Spring MVC or Rest scenarios. Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context
This link from spring official describes more about it, Spring Scopes.

Difference between Controller and restController in Spring.

@Controller is used to mark classes as Spring MVC Controller. Through Controller we can return a view using view resolver.
@RestController has @controller and with that, it adds @ResponseBody annotation as well.ResponseBody does not need a view. It uses HTTP Message converters to convert the return value to HTTP response body, based on the content-type in the request HTTP header

What is the difference between @component and @bean annotation?

@component @bean
@component is used to auto-detect and auto-configure beans using classpath scanning. @Bean is used to explicitly declare a single bean, rather than letting Spring do it automatically as @component.
There's an implicit one-to-one mapping between the annotated class and the bean (i.e. one bean per class). It decouples the declaration of the bean from the class definition and lets you create and configure beans.
@Component is a class-level annotation. @Bean is a method level annotation and name of the method serves as the bean name.
We cannot create a bean of a class using @Component if the class is outside spring. we can create a bean of a class using @Bean even if the class is present outside the spring container.
@Component has different specializations like @Controller,@RestController, @Repository and @Service. @Bean has no specializations.
We do not need @Configuration. We need to declare @Configuration annotation.

Why the exception is handled in @ControllerAdvice in Spring?

@ControllerAdice annotation is used to handle exception globally. By default, it will apply to all classes that use the @Controller annotation.
If we do not use @ControllerAdvice instead try to handle the exception in @Controller classes then we will end up creating a lot duplicate code.

Previous
Next Post »

Pages