Spring is one of the most popular Java EE frameworks for developing web applications. Spring is based on J2EE technologies like JSP and Servlet, along with the implementation of the design pattern Model, View, and Controller. With the increase in demand for spring framework across multiple end-use platforms like automotive, agriculture, manufacturing, and forestry will strike the market. Where the market size rises with a CAGR of 4.5% from 2020 to 2027. 

If you are planning to start your career in Spring framework? We have curated a list of some crucial Spring interview questions and answers that you must go through before appearing to a Spring job interview. It will surely help you to crack the interviews. 

Q1. 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.

Q2. 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

Q3. 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. It is very useful in managing projects if planned correctly.

Q4. 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

Q5. What are the benefits of Spring?

Ans. The various benefits of Spring include –

  • It is versatile and non-invasive
  • It 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.
  • It can remove the creation of factory classes.

Q6. 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.

 Also Read>> Top Java Interview Questions & Answers

Q7. What are the different types of transaction management supported by Spring?

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

  1. Programmatic transaction management
  2. Declarative transaction management

Q8.  What are the different bean scopes in Spring?

Ans. The Spring supports five bean scopes, which are:

  • Prototype
  • Request
  • Singleton
  • Global session
  • Session

 Q9. What are some of the vital Spring modules?

Ans. Some of the vital Spring modules are:

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

 Q10. Name some AOP concepts.

Ans. Different types of AOP concepts are –

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

 Q11. 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

Q12. Are Spring singleton beans thread-safe?

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

Q13. 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

 Q14. Singleton Scopes are used with?

Ans. Singleton Scope is used with Stateless session bean.

Q15. What do you mean by the introduction?

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

 Q16. What are the important JDBC API classes?

Ans. Following are some of the important JDBI API classes:

  • Simple jdbcCall
  • NamedParameterJdbc Template
  • Simple Jdbcinsert
  • Jdbc Template

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

Ans. I would go with Spring MVC with 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.

Q20. Which Object Relation Mapping (ORM) does Spring support?

Ans. Spring supports the following ORM –

  • IBatis
  • JDO
  • OJB
  • Hibernate
  • TopLink

Q21. 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

 Q23. 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.

Q24. Name some of the types of Autoproxying?

Ans. Different types of Autoproxying are –

  • Metadata autoproxying
  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator

Q25. Can you inject an empty string value in Spring?

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

Q26. 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.

Q27. What are the different types of Dependency Injection?

Ans. Dependency Injection is of three ways –

  • 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.

Q28. 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

 Q29. 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.

Q30. 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.

Q31. 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.

Q32. 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 dependency injection method.

Q33. 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.

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

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

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

Q35.  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.

Q36. How many types of Spring Bean autowiring are there?

Ans. Spring framework has four types of autowiring –

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

Q37. 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.

Q38. 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.

Q39. What is ViewResolver class?

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

Q40. 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

Q41. 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.

Q42. On what points can you apply weaving?

Ans. We can apply weaving on the following points –

  • Compile Time
  • Class load Time
  • Runtime

Q43. 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

Q44. 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.

Q45. Name different editors used in Spring Framework.

Ans. Different editors used in Spring Framework include –

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

 Q46. 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

Q47. 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.

Q48. 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.

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 in Spring?

Ans. Following are the different types of configuration elements offered by Spring:

  • 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.

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 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. 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.

Q55. Mention the classes present in spring JDBC API?

Ans. Following are the approaches for Jdbc database access provided by the Spring framework:

  • NamedParameterJdbcTemplate
  • JdbcTemplate
  • SimpleJdbcTemplate
  • SimpleJdbcIn

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

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

  • RowMapper
  • ResultSetExtractor

Q57. Explain the difference between JDBC and Spring JDBC?

Ans. Spring JDBC value-add provided by the Spring Frameworks on top JDBC layer:

  • 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

Q58. 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

Q59. Mention the?

Ans. Different types of AutoProxying are:

  • Metadata autoproxying
  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator

Q60. 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.

 Q61. What is the procedure to upload files in 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.

Q62. 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

 Q63. What are the different features in the spring framework

Ans. Following are the different features of the Spring framework:

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

Q64. Mention the five bean scopes supported by Spring?

Ans. Following are the five bean scopes supported by Spring:

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

 Q65. Mention the three ways by which injection dependencies can be done?

Ans. Following are the three ways to perform injection dependencies:

  • Interface Injection
  • Constructor Injection
  • Setter Injection