Spring Interview Questions

By | | Updated : 2021-06-02 | Viewed : 114 times

Spring Interview Questions

As a spring developer, you need to know some of the important Spring Interview Questions.

1. What is Spring Framework?

A framework means that it will come up with semi-finalized software of common functionalities where a developer can only focus on business logic. Spring framework is light weighted and open-source for implementing Web/Enterprise applications.

2. What are the features of Spring Framework?

Please find some of the features of the spring framework.

  • Loose Coupling: Loose coupling the components is one of the good techniques in implementing large-scale applications such as enterprise applications.

  • IoC Container: The application context is nothing but IoC container and it plays a very important role in managing the bean's life cycle.IOC container handles the bean's initiations, initialization, and collaboration of dependencies into other beans.

  • Inversion Of Control: Inversion of Control means spring handles the creation and instantiation of the beans as per the bean's definition which is in beans.xml or @configuration classes.

  • Dependency Injection: It is a design pattern that implements the IoC principle. Here DI decouples the components as dependencies and dependents. So using this, dependency beans can be injected into dependent beans.

  • Aspect-Oriented Programming: In the OOP model, the classes/ objects have some relationships to make modularity. However, in the AOP model, the program can be modularized.

  • Configuration: configuration which is metadata to be supplied to the configuration is required to manage the spring container.

  • Auto-Wiring: Spring automatically collaborates the dependency beans into dependent beans by reading the configurations.

  • Data Access Framework: A submodule of the Spring framework that uses to manipulate the data in the database. Here it allows accessing the database to apply curd operations.

  • Declarative Transaction Management: Declarative transaction management is very helpful in handling database transactions.

  • Spring MVC Framework: A sub-framework that helps in implementing the web application. It comes with semi-finalized code files for common functionalities such as error handling and all.

  • Spring Testing Framework: The spring framework provides a lot of options to test the implemented code with spring modules.

3. What is IoC (Inversion of Control) Container?

The application context is represented IoC container and it plays a very important role in managing the bean\'s life cycle. IOC container manages the bean\'s initiations, initialization, and collaboration beans as per the configuration defined in configuration files.

4. What do you understand by Dependency Injection?

Dependency Injection encourages the components decoupling. The entire application is segregated into smaller components. In application execution, it is required to combine all the components. So it can be done by dependency injection. Dependency Injection describes injecting the dependencies into other beans. There are three types of dependency injection in the spring framework. 1. Constructor 2. Setter Injection 3. Interface Injection

5. Explain the difference between constructor and setter injection?

  • Constructor Injection is preferable when a bean has mandatory dependencies whereas Setter Injection is preferable in the case of non mandatory dependencies.

  • Partial dependency injection is only possible with Setter Injection whereas it is not possible with Constructor Injection

  • Setter Injection overrides the Constructor Injection values.

  • Constructor Injection is preferable when the bean contains more property values. If you use Setter Injection it is required to write more setter methods.

6. What is a Spring bean?

Spring bean is a normal Java POJO class that is managed by Spring Container. Spring Container/Application Context manages the Spring bean\'s life cycle means from instantiation to destruction of the bean.

7. What are the bean scopes available in Spring?

Available Spring Bean Scopes are

  • 1. Singleton

  • 2. Prototype

  • 3. Request

  • 4. Session

  • 5. Global Session

One more scope has been introducing in Spring 3.0, which is Thread-scoped.

  • 1. Singleton: If the bean's scope is singleton then the Spring container instantiates bean object only once per Spring container. When we requested a second time for a singleton instance then the cached bean will be returned.

  • 2.Prototype: If the bean's scope is prototype then the Spring container instantiates the bean object for each request. The bean will be instantiated any number of times based on the number of bean requests.

  • 3. Request: The Request scope is related to the HTTP request. If bean's scope is Request, Spring container instantiates bean object as per HTTP request. So Bean will be created for each HTTP request. The request scope is under the context of the web application context.

  • 4. SessionThe Session is also related to HTTP Session. If bean's scope is Session then Spring container instantiates bean object as per HTTP Session. So bean objects will be created for each HTTP session. The session scope is under the context of the web application context.

  • 5. Global SessionIt is similar to the Session scope. It works with portlet-based web applications.

8. What is bean wiring in Spring?

Bean wiring means automatically detecting the relationship between beans for injecting the dependencies into other beans without using the types of injections in spring. There are four ways for autowiring the beans by Spring.

9. What are the different types of autowiring in spring?

In spring there are 5 types of autowiring modes available.

  • 1 no: It is the default autowiring mode. no mode means no autowiring. If injection/wiring is required then properties should be used for injection.

  • 2. byName: When autowiring set byName Spring injects the dependency based on the name of the dependency bean.

  • 3. byType: When autowiring set byType then Spring injects the dependencies into other bean based on the type of bean.

  • 4. constructor: When autowiring set with the constructor then Spring injects dependencies by calling the other bean constructor.

  • 5. autodetect: Spring will choose the default autowire mode which is constructor to autowire. Spring chooses the autowire mode byType in case of constructor does not use it.

10. What is Spring Bean’s life cycle?

Spring bean\'s life cycle is nothing but from bean creation to bean destruction is called the life cycle of a bean. Here spring application context which is the spring IOC container manages the bean\'s life cycle.

Some important phases of the bean life cycle are given below.

  • 1. Spring bean instantiation

  • 2. beans collaboration using injection

  • 3. custom initMethod

  • 4. Bean is ready for usage

  • 5. custom destroyMethod

11. What are the limitations of auto wiring?

The limitations of autowiring are

  • Dependency Injection using constructor or property overrides the autowiring.

  • Autowiring will not be applicable for primitive data types.

  • Instead of using autowiring better to use dependency injection explicitly. In some of the cases, there might ambiguity with autowiring using byType.

Leave A Reply