If you’re a java developer preparing to appear for an interview where the Spring framework is the main part of the tech stack, we have curated a list of popular Spring interview questions and answers that you must go through before appearing for your job interview. 

This blog will provide the latest spring interview questions all in one place. For your convenience, we have categorized the questions under the following topics:

Table of Contents: 

General Spring Interview Questions

Spring Beans Interview Questions

Spring AOP Interview Questions

Dependency Injection/ IoC Interview Questions

Spring Annotations Interview Questions

Spring Data Access Interview Questions

Spring MVC Interview Questions

Before getting started with Spring Interview Questions, let’s have a glance at the Spring Framework job market.

Spring Framework Job Market

Spring is one of the most popular Java EE (Enterprise Edition) frameworks for developing web applications. It is based on J2EE technologies like JSP and Servlet, along with the implementation of the design pattern model, view, and controller. 

Want to learn more about Spring Framework? Explore the latest online Spring courses now! 

With the increase in demand for spring framework across multiple end-use platforms like automotive, agriculture, manufacturing, and forestry, the number of job opportunities has been on the rise. Naukri.com lists over 10,000 jobs (as of December 2020) for Spring Framework. Considering the wide use of the Spring Framework in various industries, it will continue to bloom in the market.

General Spring Interview Questions

Q1. Name different components of a Spring application.

Ans. A Spring application has the following components:

  • Interface – Defines functions
  • Bean class – Consist of properties, its setter and getter methods, functions, etc.
  • Spring Aspect-Oriented Programming (AOP) – Provides the functionality of crosscutting concerns
  • Bean Configuration File – Consists of classes information and the ways to configure them
  • User program – Uses functions

Q2. What is a Spring configuration file?

Ans. The Spring configuration file is an XML file. It has information about the classes, interfaces, and their dependencies.  It suggests how classes are configured and introduced to each other. Spring Configuration file is very useful in managing projects if planned correctly.

Q3. Name various ways to use the Spring Framework.

Ans. Spring Framework can be used as –

  • Full-fledged Spring web application
  • Third-party web framework, using Spring Frameworks middle-tier.
  • Enterprise Java Bean that can wrap existing POJOs (Plain Old Java Objects)
  • Remote usage

Q4. What are the benefits of Spring?

Ans. The various benefits of Spring include –

  • It is versatile and non-invasive
  • Spring supports dependency injection and offers multiple modules to achieve different functionality for the development of the application.
  • It allows developers to develop enterprise-class applications using POJOs.
  • Spring can remove the creation of factory classes.

Q5. How can a Spring file be converted to Java?

Ans. A Spring file is converted to Java by using Spring’sClassPathXmlapplicationContext from Java to set the Spring configuration.

Q6. What are the different features of the spring framework?

Ans. Following are the different features:

  • Transaction Management
  • Inversion of Control
  • MVC framework
  • Aspect-Oriented Programming
  • Lightweight
  • Container

Also Read>> Top Java Interview Questions & Answers

Q7. What are the different types of transaction management?

Ans. There are two types of transaction management supported by Spring, that include –

  • Programmatic transaction management
  • Declarative transaction management

Q8. What are some of the vital Spring modules?

Ans. Some of the vital Spring modules are:

  • Spring AOP
  • JDBC
  • MVC
  • Web Module
  • Context
  • DAO

Q9. How can you configure Spring into the application?

Ans. There are three ways to configure Spring into the application, which are –

  • Java-based configuration
  • XML based configuration
  • Annotation-based configuration

Q10. How is Spring Java different from Node JS?

Ans. Following are some key differences between Spring Java and Node JS:

Spring JavaNode.JS
Blocking I/O and users must wait for every I/O requestApplications are non-blocking I/O (Input/Output model) and asynchronous
Multi-threaded hence tasks are performed simultaneouslySingle-threaded hence works on the concept of one request one thread
Expensive as requires multiple threadsNot as expensive

Q11. Is it possible to have multiple Spring configuration files?

Ans. Yes, it is possible to define multiple spring context configuration files through ContextConfigLocation. It can consist of multiple locations and can be separated by commas and spaces. 

Further, it is also possible to define multiple root level spring configurations or import elements in the context configuration files.  

Q12. Which channels does Spring 2.0 support?

Ans. Spring 2.0 supports the following channels –

  • Direct Channel
  • Executor Channel
  • Pollable Channel
  • Priority Channel
  • PublishSubscribe Channel
  • Queue Channel
  • Rendezvous Channel
  • Scoped Channel
  • Subscribable Channel

Q13. What are the new features in Spring 4.0?

Ans. Different features of Spring 4.0 include –

  • It manages the Spring Bean lifecycle like JNDI lookup.
  • It is used to create web applications and web services.
  • All annotations are used to create custom composed annotations.
  • It supports WebSocket-based communications in all web applications.

Q14. What do you mean by the introduction?

Ans. The introduction enables the user to add new attributes and methods to existing classes.

Q15. Name different editors used in Spring Framework.

Ans. Different editors used in Spring Framework include –

  • ClassEditor
  • CustomDateEditor
  • FileEditor
  • LocaleEditor
  • PropertyEditor
  • StringArrayPropertyEditor
  • StringTrimmerEditor
  • URLEditor

Q16. How can you control the database connection?

Ans. Database connection can be controlled by using the following classes –

  • AbstractData Source
  • Data Source Utils
  • DataSource TransactionManager
  • DriverManager DataSource
  • SingleConnection DataSource
  • SmartData Source
  • TransactionAware DataSourceProxy

Q17. What is a Property Editor?

Ans. A property editor is a feature of the JavaBeans API. This feature enables the conversion of property values to and from text values.

Q18. What is the Web Module?

Ans. The web module provides basic web-oriented integration features and helps to create a web application without XML. After using the web module, a web.xml file needs to be configured.

Q19. What is Reactive Programming?

Ans. Reactive programming is a non-blocking, asynchronous, and event-driven application. It requires a small number of threads to scale. It has a backpressure mechanism. The backpressure is on the key ingredient that ensures that the producers do not overwhelm consumers.

Q20. What are the advantages of Reactive Programming?

Ans. The following are some of the advantages of Reactive Programming: 

  • Helps to increase the utilization of computing resources, such as multicore and multi-CPU hardware.
  • Complex threading becomes very easy 
  • Helps to increase the performance by a reduction in the serialization process.
  • Easy to implement back-pressure

Spring Beans Interview Questions

 Q21. What is a Spring Bean?

Ans. Spring Bean is a crucial concept of the Spring Framework. Objects that form the base of any application and those managed by the Spring IoC container are called beAns.

Q22. Why is the Spring bean configuration file necessary?

Ans. Spring Bean configuration file can be used to define all the beans initialized by Spring Context. Spring Bean configuration file reads and initializes the Spring bean XML file when the instance of Spring ApplicationContext is created. These files can then be used to get different bean instances once the context is initialized.

Q23. What are Inner Beans?

Ans. If a bean is using the property of another bean, it can be called an inner bean. Spring’s XML based configuration metadata defines the use of <bean> element inside the <property> or <constructor-arg>. Inner beans are anonymous.

Q24. What is Bean Wiring?

Ans. If beans get combined within the Spring container, the structure is known as bean wiring. These beans are usually combined by using the dependency injection method.

Q25. What is Bean Factory?

Ans. Bean Factory is a Lightweight container that loads bean definitions and manages beAns.  These beans are configured using an XML file and manage singleton-defined bean.

Q26. What are the different ways of controlling the life cycle events of a bean?

Ans. There are 2 ways for controlling the life cycle events of a bean –

  • InitializingBean and DisposableBean interfaces
  • Init-method and destroy-method

Q27. Are Spring singleton beans thread-safe?

Ans. No, Spring singleton beans are not thread-safe.

Q28.  What is Autowiring?

Ans. Autowiring is a crucial feature in the Spring framework, which uses setter or constructor injection and allows the user to inject the object dependency implicitly.

Q29. How many types of Spring Bean auto wiring are there?

Ans. Spring framework has four types of auto wiring –

  • autowire byName
  • autowire byType
  • autowire by constructor
  • autowiring by @Autowired and @Qualifier annotations

Q30. What are the limitations with auto wiring?

Ans.The following are the limitations with auto wiring:

  • Overriding possibility: Dependencies can be specified using <constructor-arg> and <property> settings. It will always override autowiring.
  • Primitive data type: The primitive data types String or Integer using property or constructor-args tag need to be defined. These tags cannot be autowired.
  • Confusing Nature: If there is a lot of dependency in a program, then it will be hard to find using autowire attribute of the bean. Always prefer using explicit wiring because autowiring is less precise.

Q31. Mention the five bean scopes supported by Spring?

Ans. Following are the five bean scopes:

  • Request
  • Global-Session
  • Session
  • Singleton
  • Prototype

Q32. What are Singleton Scopes used with?

Ans. Singleton Scopes are used with Stateless session bean.

Q33. Explain Bean Lifecycle.

Ans. The bean lifecycle defines the path of bean initiation and lasts till bean completion. It is managed by spring containers as all the spring containers start as soon as the program is initiated. Further, as per the request, it creates an instance of a bean and then injects the dependencies. Once this process is done, the bean is being destroyed as soon as the spring container is closed. 

Below is the bean lifecycle flow:

Container started → bean instantiated → dependencies injected → custom int() method → custom utility method → custom destroy() method

Spring AOP Interview Questions

Q34. Name some AOP concepts.

Ans. Different types of AOP concepts are –

  • Interceptor
  • Joinpoint
  • Aspect
  • Target object
  • Weaving
  • Pointcut
  • Advice
  • AOP proxy

Q35. Name the types of advice in AOP.

Ans. There are five types of advice in AOP –

  • Throws advice
  • Around advice
  • After returning advice
  • Before advice
  • After advice

Q36. What is the AOP module?

Ans. Aspect-Oriented Programming (AOP) is a key component of the Spring framework. It allows interceptors to intercept an application. It can also be defined as the breaking of codes into different modules.

Q37. What is weaving?

Ans. Weaving is a process of applying aspects to the target object or other application types to create an advised object or a new proxy object.

Q38. On what points can you apply weaving?

Ans. We can apply weaving on the following points –

  • Compile Time
  • Class load Time
  • Runtime

Q39. Name some of the types of Autoproxying.

Ans. Different types of AutoProxying are:

  • Metadata autoproxying
  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator

Also check out – Top Java Developer Interview Questions and Answers

Q40. Explain JoinPoint.

Ans. JoinPoint showcases a point in an application where we can plug in an abstract oriented programming aspect. This is the actual place where actions will be taken with the help of the Spring AOP framework.

Q41. What do you mean by Spring Boot?

Ans. From the Spring umbrella, a Spring Boot is a Java-based framework that helps in creating microservices that helps in building stand-alone spring applications that are production-ready. Some of the advantages of using a Spring Boot framework are:

  • Eases the process of developing spring applications
  • Boosts productivity
  • Reduces development time of application 
  • Offers flexibility to configure with XML and Java Beans

Dependency Injection/ IoC Interview Questions

Q42. What is Dependency Injection?

Ans. Dependency Injection is one of the important functionalities of Spring IoC (Inversion of Control) and is responsible for injecting dependencies through different processes, either through the Constructor or Setter method. Dependency Injection also ensures loose coupling between the classes.

Q43. What are the different types of Dependency Injection?

Ans. Dependency Injection is of three types –

  • Constructor injection – Here a class constructor provides dependencies
  • Setter injection – Here, the client exposes a setter method used by the injector to inject the dependency.
  • Interface injection – Here the injector method provides dependency

Only constructor and setter injections are used in the Spring framework.

Q44. What are the benefits of using IoC?

Ans. The primary benefits of IoC include –

  • Minimizes the number of codes in any application
  • Makes a platform/application easy to test, as it doesn’t need singletons or JNDI lookup mechanisms
  • Promotes loose coupling with fewer efforts and intrusive mechanism
  • Facilitates Eager Instantiation

Q45. Can you inject an empty string value?

Ans. Yes, we can inject an empty string value as the default value.

Q46. What do you mean by loose coupling?

Ans. Coupling is associated with collaboration, and it is basically of two types – tight coupling and loose coupling. 

Loose Coupling – It refers to the concept of removing one object’s dependency on another object (ClassX’s dependence on ClassY). It generally helps in:

  • Improving test-ability
  • Following GOF principle
  • Swapping pieces of codes and objects

Tight Coupling – This form of coupling refers to the concept where when one object is changing, the other one is also compelled to change. 

Q47. What is the difference between constructor injection and setter injection?

Ans. The differences between constructor injection and setter injection are: 

Constructor InjectionSetter Injection
No partial injection.Partial injection can be present.
It does not override the setter property.It overrides the constructor property.
A new instance is created if any modification is done.New instance is not created if any modification is done.

Q48. State difference between BeanFactory and ApplicationContext.

Ans. Spring involves two different IoC containers naming BeanFactory and other is ApplicationContext: Below are some key differences between both of the IoC containers:

BeanFactoryApplicationContext
No support for Annotation based dependency injectionSupports annotation
Does not supports publishing event to beansSupports publishing event to beans 
Does Not support internationalizationSupports internationalization
Slow loadingFast loading

Q49. What is DataAccessException?

Ans. DataAccessException is an unchecked RuntimeException. It is used to handle the errors occurring when the details of the database access API are being used.

Q50. How will you access Hibernate using Spring?

Ans. We can access Hibernate using Spring via two ways –

  • Inversion of Control with a Hibernate Template and Callback
  • Extending HibernateDAOSupport and Applying an AOP Interceptor node

Q51. What is the procedure to provide configuration metadata to the Spring Container?

Ans. Following are three important methods to provide configuration metadata to the Spring Container:

  • Annotation-based configuration
  • XML based configuration file
  • Java-based configuration

Q52. What is the procedure to inject a Java Collection?

Ans. Following are the different types of configuration elements:

  • The <set> type is used to inject a set of values without any duplicate values.
  • The <list> type is used to inject a list of values, in this case, duplicates are allowed.
  • The <props> type can be used to inject a collection of name-value pairs, in this case, both name and value should be Strings.
  • The <map> type is used to inject a collection of name-value pairs, in this case, name and value can be of any type.

Spring Annotations Interview Questions

Q53. What are some of the important Spring annotations?

Ans. List of some of the Spring annotations are:

  • @Service is used to indicate a class as a Service. 
  • @Scope  is used to configure the scope of the spring bean.
  • @Configuration is used to indicate that the class can be used as a source of bean definitions by the Spring IoC container.
  • @ComponentScan is used to scan all the classes available under a package.
  • @Required used to indicate the affected bean property that must be populated at configuration time, by an explicit property value in a bean definition or with the help of autowiring. 
  • @ResponseBody is used to send an object as a response, basically used for sending XML or JSON data as a response.
  • @PathVariable is used to map dynamic values from the URI to handler method arguments.
  • @Autowired is used for autowire bean on the setter method similar to @Required annotation, on the constructor, on a property, or on methods with arbitrary names and/or multiple arguments.
  • @Component is used to indicate a class as a component, where all the classes are used for auto-detection and composed as a bean when annotation-based configurations are used.
  • @Controller is used for MVC applications and is generally used with @RequestMapping annotation.
  • @Repository annotation indicates that a component is used as a mechanism to store/retrieve/search data.
  • @Qualifier is used for wiring a single bean with a property at a time when there are multiple beans of similar type with the help of @Autowired annotation
  • @Bean is used in java based configurations, where a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.

Q54. Can you name different types of events of Listeners?

Ans. Yes, there are three types of events of listeners:

  • ContextClosedEvent – When the context is closed.
  • ContextRefreshedEvent – When the context is initialized or refreshed
  • RequestHandledEvent – When the web context handles a request

Q55. What is the annotation-based (@AspectJ based) aspect implementation?

Ans. The given implementation case (@AspectJ based implementation) leads to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.

Q56. How to go ahead with exceptional handling in Spring?

Ans. There are multiple ways to handle exception in Spring and some of them are:

  1. Controller-based annotation method using @ExceptionHandler annotation
  2. Global Exception Handler along with  @ControllerAdvice annotation
  3. HandlerExceptionResolver implementation for creating global exception handler

Q57.  How to send an email by using Spring Framework?

Ans. The JavaMailSenderImpl annotation of Spring helps in easily sending emails from Java. This JavaMailSenderImpl produces a JavaMailSender interface and supports:

  • JavaMail API’s MimeMessages 
  • Spring’s SimpleMailMessage

Further, it allows the class to pre-define the mail settings pre-configure JavaMail Session. The below steps will help in sending an email using the Spring framework:

  1. In ApplicationContext.xml – Configure JavaMailSenderImpl as Spring bean
  2. With JavaMailSenderImpl reference, configure the test application
  3. Create a Java application to send an email

Note: If you are not very comfortable with writing Spring configuration file, you can sign-up for Spring online course like Spring Framework 5: Beginner to Guru on Udemy as it thoroughly covers Spring Framework concepts)

Q58. How are The Spring @Controller and @RestController Annotations different from each other?

Ans. Below are the differences:

@Controller@RestController
A very traditional Spring controllerIntroduced in Spring 4.0 to offer effective RESTful web services.
Includes only the components of @ControllerCombines @Controller and @ResponseBody
Designates the bean classEliminates annotation needs for handling every request of the controller class

Q59. Is it possible to send an object as the response of the Controller handler method?

Ans. Yes, it is possible to send JSON or XML based response in restful web services, with the help of @ResponseBody annotation.

Data Access Interview Questions

Q60. What is Spring DAO?

Ans. In Spring, DAO stands for Data Access Object which is a design pattern that provides an abstract interface to some type of database. The basic objective of DAO is to ease the data access technologies including JDBC and Hibernate. It offers data operations without exposing the exact database details. Further, a DAO pattern is used to perform CRUD operations. 

Q61. Name some classes present in spring JDBC API?

Ans. The following are some of the important JDBI API classes:

  • NamedParameterJdbc Template
  • Jdbc Template
  • SimpleJdbc Template
  • SimpleJdbcInsert 
  • SimpleJdbcCall

Q62. What is the possible way to fetch records by Spring JdbcTemplate?

Ans. The following are the two interfaces that can be used to fetch records from the database:

  • RowMapper
  • ResultSetExtractor

Q63. How is Spring JDBC different from plain JDBC?

Ans. Spring JDBC is a value-add provided by the Spring Frameworks on the top JDBC layer. It is an abstraction layer on top of JDBC technology. Spring JDBC does the following: 

  • Do the work for each iteration
  • Prepare and execute the statement
  • Process any exception
  • Define connection parameters
  • Handle transactions
  • Set up the loop to iterate through the results (if any)
  • Open the connection
  • Specify the statement
  • Close the connection

Q64. What ORM’s are supported by Spring?

Ans. Spring supports the following ORM’s:

  • TopLink
  • Hibernate
  • JPA (Java Persistence API)
  • iBatis
  • OJB
  • JDO (Java Data Objects)

Q65. How would you access hibernate with the help of Spring?

Ans. There are two ways to hibernate using Spring:

  • By extending hibernateDaoSupport and spreading over an AOP interceptor
  • By inversion of control (IOC) with a HibernateTemplate and Callback

Q66. How is the context:annotation-config and context:component-scan different from each other?

Ans. Both of context:annotation-config and context:component-scan are the two different XML configuration elements of Spring. 

 <context:annotation-config> is used for:

  • Activating annotations in beans that are registered in the application context
  • Activating dependency injection annotations
  • Resolving dependencies like @Autowired, @Qualifier, @PostConstruct, @PreDestroy, and @Resource 

<context:component-scan> is used for:

  • Identifying the right packages which need to be scanned and find annotated beans or components
  • Detecting @Component, @Repository, @Service, @Controller, @RestController, and @Configuration

MVC (Model-View-Controller) Interview Questions

Q67. What is Spring MVC?

Ans. Spring MVC follows the Model-View-Controller design pattern and implements basic features of a core Spring framework, including Inversion of Control and Dependency Injection.

Q68. How is the DispatcherServlet used in Spring MVC?

Ans. The DispatcherServlet is a popular Spring MVC web framework that acts as Front Controller. A Front Controller receives all the information related to incoming requests and further delegates this request to other components (@Controller annotation & InternalResourceViewResolver class). 

Also, it is noticeable that a DispatcherServlet aptly handles incoming requests and forwards them to the right Controller depending upon its actual role. It also plays a crucial role in activities like:

  • View resolution
  • Error handling
  • Locale resolution
  • Theme resolution

Q69. Give steps to configure DispatcherServlet in Spring. 

Ans. Steps to configure DispatcherServlet in Spring:

<servlet> 

<servlet-name>dispatcher</servlet-name>

<servlet-class> 

org.springframework.web.servlet.DispatcherServlet 

</servlet-class> 

<init-param> 

<param-name>ConfigLocation</param-name> 

<!–Defaults to ABC.xml –> <param-value>ABC.xml

</param-value> 

</init-param> 

</servlet> 

Q70. What is Thymeleaf?

Ans. Thymeleaf is a server-side template of Java library which is best suited for XHTML/HTML5 in web applications. However, it can process any XML file and the best part about Thymeleaf is that it can process seamless templates for:

  • Valid XML
  • XHTML
  • Valid XHTML
  • HTML5
  • Legacy HTML5

Q71. What is the procedure to upload files in the Spring MVC Application?

Ans. Spring MVC Application provides built-in support to upload files by using MultipartResolver interface implementations. Here, a validator is used to check the file uploads in the field, which should consist of a size greater than zero. In the end, there is a simple view that consists of a form with the option to upload a file.

Q72. How do you validate from data in Spring Web MVC Framework?

Ans. There are three  ways to implement validation: 

  • By using annotation
  • Manually
  • By using both functions

Q73. What is a RowCallbackHandler?

Ans. RowCallbackHandler is an interface used by JdbcTemplate to process rows of a ResultSet. It processes rows on a per-row basis.

Q74. What is ViewResolver class?

Ans. The ViewResolver class offers to map between view names and actual views. It also allows the user to set properties like prefix or suffix to the view name.

Q75. Which would you consider, Play framework or Spring MVC?

Ans. I would go with Spring MVC for these reasons:

  • Availability of ample resources on Spring MVC.EBOOK
  • It renders a very clean division among views, controllers, and JavaBean models.
  • Truly view-agnostic
  • It has a better testable code and ensures better integration with view technologies.

Q76. Differentiate between SpringBoot and Spring MVC.

Ans. The differences between SpringBoot and Spring MVC are: 

SpringBootSpring MVC
It is a way to ease the creation of a stand-alone application with basic configurationsSpring MVC is a module of Web-Servlet which is used for implementing a web application
It offers a unique feature like auto-configuration of applicationIt is equivalent to JSF(Java Server Faces) in the JavaEE stack
SpringBoot offers embedded HTTP servers such as Jetty, Tomcat for creating executable filesIt has an equivalent @RestController for implementation of REST 

All the Best for your interview and watch out this space for more Spring interview questions and answers!

 

————————————————————————————————————–

If you have recently completed a professional course/certification, click here to submit a review and get FREE certification highlighter worth Rs. 500.

4.83 avg. rating (96% score) - 12 votes