preloader
Spring Interview Questions

Top 50 Spring Interview Questions and Answers, Prepare Now

author image

Spring is one of the important topics to be asked in the technical job interview of Software engineers, developers, etc. posts. Candidates can prepare their Spring Interview Questions from this page. Here we have given 50 Spring Interview Questions and Answers which can be asked in the job interview. Read on to know more about Spring topics and coding from the below section.

About Spring: The spring s an open-source powerful java framework that is used for developing a web application and control container for the Java platform. Any Java application can use the spring for development, but there is an extension for developing web applications on top of the Java EE platform.

Spring Interview Questions

1. What do you understand by Spring Framework?

2. Tell some advantages of the spring framework?

3. Tell the modules of the spring framework?

4. What is the IOC container responsible for in spring?

5. Name the types of IOC containers in spring?

6. Explain the Spring configuration file?

7. Explain Spring Boot?

8. Explain some features of Spring Framework?

9. Explain the IoC (Inversion of Control) Container?

10. Explain Dependency Injection?

11. Differentiate constructor and setter injection?

12. Define Spring Beans?

13. Explain the bean scopes available in spring?

14. Explain Bean Wiring?

15. Tell the way to disable specific auto-configuration classes?

16. Are we able to disable the default web server in the Spring Boot application?

17. Differentiate between BeanFactory and ApplicationContext?

18. When will you use singleton and prototype scope?

19. Explain the transaction management supports by spring?

20. Name the classes for spring JDBC API?

21. Tell the ways to fetch records by spring JdbcTemplate?

22. Tell the use of NamedParameterJdbcTemplate?

23. Tell the advantage of SimpleJdbcTemplate?

24. Explain AOP?

25. Tell some advantages of spring AOP?

26. Explain AOP terminology?

27. Name the types of advice in AOP?

28. Name three AOP implementations?

29. Tell some limitations of autowiring?

30. How to exclude any package by not using the basePackages filter?

31. What are the advantages of JdbcTemplate in spring?

32. How to get ServletConfig and ServletContext objects in spring bean?

33. Does all JoinPoints are supported by the spring framework?

34. Tell us how the root application context in Spring MVC loaded?

35. Is singleton bean really thread-safe?

36. What are the ways to thread-safety in beans?

37. Explain the significance of @Repository annotation?

38. Explain the importance of @Required annotation?

39. Explain the importance of session scope?

40. Explain the role of @ModelAttribute annotation?

41. Tell the importance of the web.xml in Spring MVC?

42. Explain the Model in Spring MVC?

43. Explain the usage of @Controller annotation?

44. How will you create a controller without using the given @Controller or @RestController annotations?

45. Explain the Spring MVC framework?

46. Name the front controller class of Spring MVC?

47. Explain @Controller annotation?

48. Explain the ViewResolver class?

49. Name the ViewResolver class that is widely used?

50. Define Pointcut?


Learn More Interview Questions Here:


Spring Interview Questions and Answers

1. What do you understand by Spring Framework?

Spring is an open-source powerful java framework that is loosely coupled and lightweight used for developing simple enterprise-level applications. Spring framework is also known as “framework of frameworks” as it gives support to many other important frameworks like Structs, JSF, EJB, Hibernate, etc. It handles all the infrastructure-related development which allows the developer to focus just on application development.

2. Tell some advantages of the spring framework?

  • Loose Coupling
  • Predefined Templates
  • Declarative support
  • Easy to test
  • Powerful Abstraction
  • Lightweight
  • Fast Development

3. Tell the modules of the spring framework?

  • Test
  • Spring Core Container
  • AOP, Aspects, and Instrumentation
  • Data Access/Integration
  • Web

4. What is the IOC container responsible for in spring?

  • create the instance
  • assemble the dependencies
  • configure the instance

5. Name the types of IOC containers in spring?

  • BeanFactory
  • ApplicationContext

6. Explain the Spring configuration file?

It is an XML file that has the class’s information and defines how those classes are configured and connected. The XML configuration files are verbose and cleaner.

7. Explain Spring Boot?

It is an open-source, java-based framework that helps in Rapid Application Development and provides a platform used for the development of stand-alone and production-ready spring applications with minimal requirement of configurations.

8. Explain some features of Spring Framework?

  • Spring framework uses a layered architecture pattern that allows the selection of the necessary components and gives a robust and cohesive framework for J2EE app development.
  • The AOP (Aspect Oriented Programming) is a part of spring that supports unified development by separating application business logic from other system services.
  • It gives a highly configurable MVC web application framework that allows one to easily switch to other frameworks.
  • It provides the lifecycle of application objects by defining, creating, and managing the configurations.
  • It also follows a special design principle called IoC (Inversion of Control) that gives dependencies to objects instead of looking for creating dependent objects.
  • It is an open-source, lightweight, java-based framework.

9. Explain the IoC (Inversion of Control) Container?

Spring container works as the foundation of Spring Framework. They are used for Dependency Injection (DI) which manages the application components by making objects, linking them together along with configuration, and managing their overall life cycles. XML configuration, Java annotations, or Java code provides the instructions for the spring container to do the tasks.

10. Explain Dependency Injection?

The simple idea behind using Dependency Injection (DI) is that you don’t need to create your own objects and you just need to define the requirements of creation. We do not need to connect components and services to code. The IoC container available in Spring will connect them together.

11. Differentiate constructor and setter injection?

  • In setter injection, partial injection is allowed whereas it is not allowed in constructor injection.
  • The setter injection can override the constructor property whereas the same is not true for constructor injection.
  • Setter injection does not allow the creation of a new instance whereas constructor injection allows the creation of a new instance when modification is done.
  • If the bean uses few properties, then setter injection is used. If it has many properties, then constructor injection is preferred.

12. Define Spring Beans?

They form the objects which are called the backbone of the user’s application and are controlled by the Spring IoC container. Spring beans are configured, instantiated, wired, and controlled by an IoC container.

13. Explain the bean scopes available in spring?

  • Singleton: It is a single instance per IoC container.
  • Prototype: It is a single bean definition that can be any number of object instances.
  • Request: It makes an HTTP request.
  • Session: It makes an HTTP session.
  • Global-session: It makes a Global HTTP session.

Note: The last three-bean scopes are only available when the users use web-aware ApplicationContext containers.

14. Explain Bean Wiring?

When beans are connected together in the spring container, they are kind of wired so the phenomenon is called bean wiring. The spring container must-have information on what beans are required and how they are dependent on each other. This can be done by means of XML / Annotations / Java code-based configuration.

15. Tell the way to disable specific auto-configuration classes?

By using the exclude attribute of @EnableAutoConfiguration as shown below:

@EnableAutoConfiguration(exclude = {InterviewBitAutoConfiguration.class})

If the class is not mentioned on the classpath, we can state the fully qualified name as the value for the excludeName.

//By using “excludeName”

@EnableAutoConfiguration(excludeName={Foo.class})

You can add into the application.properties and multiple classes can be added by keeping it comma-separated.

16. Are we able to disable the default web server in the Spring Boot application?

Yes! application.properties are used to arrange the web application type, by mentioning spring.main.web-application-type=none.

Spring Interview Questions For Experienced

17. Differentiate between BeanFactory and ApplicationContext?

ApplicationContext is the advanced container whereas BeanFactory is the simple container. ApplicationContext is the extended version of the BeanFactory interface. ApplicationContext gives more services and features than BeanFactory like integration with spring AOP, message resource handling for i18n, etc.

18. When will you use singleton and prototype scope?

Singleton scope will be used with EJB stateless session bean and prototype scope will be used with EJB stateful session bean.

19. Explain the transaction management supports by spring?

  • Programmatic Transaction Management: It is used for a few transaction operations.
  • Declarative Transaction Management: It is used for many transaction operations.

20. Name the classes for spring JDBC API?

21. Tell the ways to fetch records by spring JdbcTemplate?

By using the query method of JdbcTemplate you can fetch records from the database. There are two interfaces that are used:

  1. ResultSetExtractor
  2. RowMapper

22. Tell the use of NamedParameterJdbcTemplate?

To pass values to the named parameter NamedParameterJdbcTemplate class is used. A named parameter is much used and better than ? (question mark of PreparedStatement).

23. Tell the advantage of SimpleJdbcTemplate?

The SimpleJdbcTemplate uses the feature of var-args and autoboxing.

24. Explain AOP?

Aspect-Oriented Programming (AOP) is a method that splits the program logic into different pieces or parts or concerns. It enhances the modularity and the key unit is Aspect.

25. Tell some advantages of spring AOP?

Aspect-Oriented Programming (AOP) allows you to dynamically add or eliminate concern after or before the business logic. It is easy to plug and maintain.

26. Explain AOP terminology?

Aspect-Oriented Programming terminologies or concepts are as follows:

  • JoinPoint
  • Advice
  • Pointcut
  • Aspect
  • Introduction
  • Target Object
  • Interceptor
  • AOP Proxy
  • Weaving

27. Name the types of advice in AOP?

There are basically 5 types of advice in spring AOP.

  1. Before Advice
  2. After Advice
  3. After Returning Advice
  4. Throws Advice
  5. Around Advice

28. Name three AOP implementations?

  • Spring AOP
  • Apache AspectJ
  • JBoss AOP

29. Tell some limitations of autowiring?

  • Overriding possibility: The dependencies are mentioned using and settings that override auto wiring.
  • Data types restriction: In this primitive data types, Strings, and Classes can’t be auto wired.

30. How to exclude any package by not using the basePackages filter?

By using the exclude attribute in the annotation @SpringBootApplication we can exclude any package:

@SpringBootApplication(exclude= {Student.class})

public class InterviewBitAppConfiguration {}

Spring Interview Questions For 5 Years Experience

31. What are the advantages of JdbcTemplate in spring?

Less Coding is required as using the JdbcTemplate class reduces the coding and you don’t need to create a statement, connection, commit a transaction, start a transaction, and close connection to execute multiple queries. You can directly execute these queries using JdbcTemplate.

32. How to get ServletConfig and ServletContext objects in spring bean?

There are two ways, first is by implementing the spring-aware interfaces or using @Autowired annotation.

@Autowired

private ServletContext servletContext;

@Autowired

private ServletConfig servletConfig;

33. Does all JoinPoints are supported by the spring framework?

No, the spring framework only supports and allows method execution joinpoint.

34. Tell us how the root application context in Spring MVC loaded?

The root application context uses the ContextLoaderListener to load the application. Spring MVC enables instantiating multiple DispatcherServlet and individually they have multiple contexts definite to them.

35. Is singleton bean really thread-safe?

No, the singleton beans are not thread-safe at all because it mainly deals with the program execution and the singleton is a simple design pattern used for the creation of objects. Thread safety nature of a bean is based on the nature of its implementation.

36. What are the ways to thread-safety in beans?

The thread safety in spring beans can be achieved by modifying the scope of the bean to request or prototype or session by costing the performance. It totally depends on the project requirements.

37. Explain the significance of @Repository annotation?

@Repository annotation shows that a repository component is used which acts as a means to store, search or retrieve data. These annotations can be added to the DAO classes.

38. Explain the importance of @Required annotation?

@Required annotation is used to represent that the bean property can be populated through autowiring or any explicit value during the bean defining the configuration time. For example, a code snippet where we require to have the values of age and the name:

import org.Springframework.beans.factory.annotation.Required;

public class User {

private int age;

private String name;

@Required

public void setAge(int age) {

this.age = age;

}

public Integer getAge() {

return this.age;

}

@Required

public void setName(String name) {

this.name = name;

}

public String getName() {

return this.name;

}

}

39. Explain the importance of session scope?

Session scopes create bean instances for HTTP sessions. This means that a single bean can be used for the allocation of multiple HTTP requests. A scope attribute or @Scope or @SessionScope annotation is used to define the scope of the bean.

  • Using scope attribute:
  • Using @Scope annotation:

@Component

@Scope(“session”)

public class UserBean {

//some methods and properties

}

  • Using @SessionScope:

@Component

@SessionScope

public class UserBean {

//some methods and properties

}

40. Explain the role of @ModelAttribute annotation?

The @ModelAttribute annotation plays a very vital role in binding method parameters to the particular attribute that responds to a model. The role of the @ModelAttribute annotation is based on what purpose the developer is using it. Generally, it is used at the method level, and then it is responsible for adding attributes to it. When used at a parameter level, it shows that the parameter value is needed to be retrieved from the model layer.

You may also prepare:

41. Tell the importance of the web.xml in Spring MVC?

web.xml is also called the Deployment Descriptor which defines the servlets and their filters, mappings, and lifecycle. It also configures the ContextLoaderListener. Whenever the application is organized, a ContextLoaderListener instance is made by Servlet container that leads to a load of WebApplicationContext.

42. Explain the Model in Spring MVC?

It is a reference to the data available for rendering. It is always made and goes through to the view in Spring MVC. If a mapping controller method has a Model as a factor, then that model instance is injected into that particular method automatically. Any attributes fixed on the injected model would be conserved and passed to the View.

43. Explain the usage of @Controller annotation?

The @Controller is like a stereotype Spring MVC annotation used to define a Controller.

44. How will you create a controller without using the given @Controller or @RestController annotations?

By using the @Component annotation in the Spring MVC Controller classes we can use the controller without using the given @Controller or @RestController annotations. In this situation, the @RequestMapping annotation method is used to create a request mapping to the handler.

45. Explain the Spring MVC framework?

It is a request-driven framework and one of the major components of the spring framework. It uses loosely coupled elements and components that help the developers in creating flexible and robust web applications. The MVC (Model - View - Controller) structure splits and gives loose coupling between the various aspects of the application – input logic (Model), business logic (Controller), and UI logic (View).

46. Name the front controller class of Spring MVC?

The front controller class used in Spring MVC is DispatcherServlet class.

47. Explain @Controller annotation?

The @Controller annotation set the simple class as controller class after applying to a particular class.

48. Explain the ViewResolver class?

It resolves the View component to be summoned for the request. To resolve the view component it defines prefix and suffix properties.

49. Name the ViewResolver class that is widely used?

The org.springframework.web.servlet.view.InternalResourceViewResolver is the ViewResolver class that is widely used.

50. Define Pointcut?

It is an expression language of Spring AOP (Aspect Oriented Programming).

We hope that these spring interview questions and answers will help you in cracking your job interview. If you have any queries or questions then reach us through the below-given comment box.

Want to prepare for these languages:

Recent Articles