This post is the part of our Interview Questions series and in this post, we will discuss some of the commonly asked Spring Interview Questions during a job interview.
Spring Beans are the core of Spring Framework. Some questions related to Spring Beans are:
Spring is known as one of the most popular frameworks for Java Enterprise applications. Spring framework Dependency injection feature can be used on any Java based application (web or non-web).
The core of Spring framework is based on dependency injection design pattern which allows us to easily build decoupled systems.
Spring Framework makes it easier to build enterprise Java applications, below are some of the advantages of using it
@Injectannotation which is not Spring based annotation, there will be no change in case we want to use Guice as our dependency injection API.
Dependency Injection is a technique with which we can make our program loosely coupled and inject required dependencies by defining them as Beans using several annotations like
@Controller or a
Spring supports three types of Dependency Injection
Spring support below 7 scopes. We can also define custom scope if required.
For more details, read our article Spring Bean Scopes
Bean scopes define the lifecycle of a Bean.
For more information read Spring Bean Scopes
As of Spring 2.0, the bean scoping mechanism in Spring is extensible. This means that you are not limited to just the bean scopes that Spring provides out of the box; you can define your own scopes.Read Custom Scope in Spring to learn how to create and use custom scope in your application.
The recommended way to inject dependencies into Spring components is by using constructor injection. This should be followed by any compulsory dependencies needed. For optional dependencies, setter injection should be used.
Spring profiles allow us to configure Spring Beans based on the profile of dev, QA, staging, production etc. The use of profiles is that we can make same Spring app which points to different databases or message broker instances with a difference of just a flag. Please read our article on Spring Profiles for more detail.
@Required annotation marks a bean as a required bean which means it needs to be configured at configuration time. Otherwise, the Spring container will throw a BeanInitializationException exception.
@Autowired annotation is used with fields or methods for injecting a bean by type. This annotation allows Spring to resolve and inject collaborating beans into the required bean.
@ExceptionHandlerannotation at the Controller level. This helps in managing an exception in a particular controller without affecting complete app flow.
@ControllerAdviceannotation, we can configure Exception handlers for application-level control and still keep the code very clean and modular.
Out of all,
@Component annotation is a generic annotation. So anything which is a Controller, Service or a Repository, it’s a Component as well. For specifics, let’s look at each of one of these:
@Controller: This annotation enables the detection of
@RequestMappingannotation in the class. It also indicates that this class is a Controller.
@Service: It indicates that this class handles the business logic of the application and ought to call the @Repository classes for DB interaction.
@Repository: This indicates that the class defines a DB interface. It makes sure that the platform-specific exceptions are caught again and thrown again as Spring specific unchecked exceptions
There is a minimal similarity between Singleton Design Pattern and Spring Singleton Scope.Singleton Design Pattern as defined in the Gang of Four(GoF) books refer to the scope of the object of only one instance per ClassLoader. The scope of Spring Singleton bean is referred to as “a single instance per bean and per container” . To understand it more clearly, let’s take a look at the below example
<!-- Singlton is the default scope, you can skip scope attribute in below declaration--!> <bean id="bean1" class="com.javadevjournal.SingletonBean" scope="singleton"/> <bean id="bean2" class="com.javadevjournal.SingletonBean" scope="singleton"/>
In the above example, Spring Container will create 2 instances of our SingletonBean class, to put it, in other words, Spring container will create 2 instances of our class and bind them with the “id” and store them in singleton cache. Any call for the system with id=”bean1″ will return bean instance from singleton cache bind with Id “bean1” and call for id=”bean2″ will return instance bind with id “bean2”.
Read Spring Singleton vs Singleton Pattern for more detail.
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.The Spring Beans are Java Objects that are initialized by the Spring IoC container.Any normal Java POJO class can be a Spring Bean if it’s configured to be initialized via container by providing configuration metadata information.
In this article, we discussed some the commonly asked Spring Interview Questions during a job interview.We will be covering Interview questions of the other modules of the Spring framework in a different post.