Spring Interview Questions and Answers

Last Updated : 25 Oct, 2025

Spring is a widely used Java framework known for its flexibility, modularity, and enterprise-grade features. Top companies like Uber, Google, Netflix, and Amazon rely on Spring for its performance and scalability. This guide covers essential Spring concepts, advanced topics, and common interview questions for freshers and experienced professionals.

Spring Core, IoC, and Dependency Injection

1. What is Spring Framework

Spring is an open-source Java framework that simplifies enterprise application development by handling infrastructure concerns and allowing developers to focus on business logic. Its key features include IoC (Inversion of Control), DI (Dependency Injection), and modular design.

2. Versions Overview:

  • Spring 2.5 (2007): Annotation support
  • Spring 3.0 (2009): Spring Expression Language, profiles
  • Spring 4.0 (2013): Java 8 support, WebSocket module
  • Spring 5.0 (2017): WebFlux, Kotlin support
  • Spring 6.x (2022–2024): Java 17, Jakarta EE 9+, cloud-native enhancements

3. Key features

  • Modular design
  • Dependency Injection
  • Aspect-Oriented Programming (AOP)
  • Transaction management
  • Data access
  • MVC framework
  • Web development
  • Testing support
  • Spring Cloud integration

4. Advantages

  • High productivity: Reduced boilerplate and simplified testing
  • Easy maintenance: Loose coupling, separation of concerns
  • Security: Built-in authentication, authorization, data protection
  • Community support: Large active community

5. Spring Application Configuration

  • Annotations: Concise, limited flexibility for large apps
  • XML: Centralized, less maintainable
  • Java Configuration Classes: Using @Configuration and @Bean
  • Property Sources: Externalized configuration
  • Spring Boot: Simplified auto-configuration

6. Inversion of Control(IoC)

Transferring dependency management from the application to the container improves scalability and testability.

IoC Containers:

  • BeanFactory: Basic container for object creation and DI
  • ApplicationContext: Advanced container supporting events, lifecycle management, and resource access

3

7. Dependency Injection(DI)

Injecting dependencies into beans automatically.

Types:

  • Constructor injection: injection using constructor
  • Setter injection: using setter methods
  • Field injection: directly into the fields

8. Metadata Types

  • Annotations (@Component, @Service, etc.)
  • XML configuration
  • Java configuration (@Configuration, @Bean)
  • Property sources

9. Spring Beans and scopes

  • Singleton: Single instance per container
  • Prototype: New instance per request
  • Request: New instance per HTTP request
  • Session: New instance per user session

10. Bean LifeCycle:

  • Bean Instantiation: Creation of bean class instance.
  • Bean Post-processing: Use of post-processors for customizing the beans.
  • Bean Initialization: Use of @PostConstruct to set up the beans using methods.
  • Bean Usage: Injection of beans for application-wide use
  • Bean Destruction: Destroys the bean through methods annotated with @PreDestroy

1

11. Autowiring

Automatic injection of dependencies. Types: no autowiring, by name, by type, constructor.

Spring Boot

12. What is Spring Boot

Spring Boot simplifies Spring application development by reducing configuration effort and providing auto-configured beans and embedded servers.

Advantages:

  • Minimal configuration
  • Embedded server (Tomcat)
  • Starter POMs for dependencies
  • Rapid development
  • Cloud-friendly

13. Spring vs Spring Boot

FeatureSpringSpring Boot
FocusGeneral frameworkSimplified application development
ConfigurationExtensive XMLAuto-config, minimal setup
ServerExternal requiredEmbedded server
Application typeMicroservices or monolithMicroservices optimized, monolith supported

14. Common Spring Boot Annotations

  • @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
  • @Autowired for DI
  • @RestController = @Controller + @ResponseBody
  • @Bean for bean registration

15. Spring Boot Configuration Types

  • Default: Provided by libraries
  • Custom: Overridden via properties, annotations, environment variables
  • External: From cloud or external repositories

16. Tomcat Role in the Spring Boot Application

Spring Boot includes an embedded Tomcat server by default. This server is responsible for:

  • Receiving and processing HTTP requests acts as a bridge between the user and the application.
  • Managing web resources like HTML files, and JSPs for dynamic content generation.
  • Built-in authentication which can be modified by Spring Security.

17. Profiles in Spring Boot

In Spring Boot, Profiles allow configuration for applications differently in different environments, such as

  • Development
  • Staging
  • Production

Separate configuration files are defined for each profile, which can be activated using environment variables or command-line arguments.

18. Actuator and its usage in Spring Boot

Spring Boot Actuator provides a RESTful API for monitoring and managing Spring Boot applications. These endpoints provide information about applications that can be used to optimize resources and debug issues, including:

  • Environment variables
  • Thread dumps
  • Health checks
  • Metrics
  • Beans
  • Logs

2-(1)

AOP, Hibernate, JDBC

19. Spring AOP and proxy pattern

  • AOP handles cross-cutting concerns (logging, security, transactions)
  • Proxy pattern intercepts method calls to apply aspects dynamically

20. Key components of AOP

  • Aspect: Bundles cross-cutting concerns
  • Advice: Code executed before/after/around methods
  • Pointcut: Defines join points
  • Join Point: Specific points in execution (method calls, object creation)
  • Weaving: Integrates aspects (compile, load, runtime)

21. Spring AOP vs AspectJ AOP

FeatureSpring AOPAspectJ AOP
Programming modelAnnotation or XML configuration supportedDedicated AspectJ compiler
WeavingDynamic proxy weaving at runtimeruntime weaving supported
Supported featuresAspect composition, pointcuts, advice, etccontrol flow join and aspect inheritance

22. Advantages of AOP and its implementation

AOP helps to maintain, modify, and understand code easily.

  • Modularization: separation of concerns like logging, security, etc from core business logic, to increase maintainability.
  • Reusability: Bundles concerns in reusable aspects, improving code reusability.
  • Interception: Allows interception and modification of method calls, enabling features like logging, security, and caching.

23. Hibernate ORM

  • ORM framework mapping Java objects to DB tables
  • Access via Spring Data JPA, HibernateTemplate, or JdbcTemplate

24. Explain Hibernate Validator Framework and HibernateTemplate class

Hibernate Validator Framework: Provides validation against defined constraints, and prevents invalid data from entering the application.

A few examples are listed:

  • @NotNull
  • @Size
  • @Email

HibernateTemplate class: Provides an interface for data access operations like given below, without writing SQL queries.

  • get
  • load
  • save
  • update
  • delete

25. Explain Spring JDBC API and its classes.

Spring provides a simple way in the form of a JDBC abstraction layer to establish a bridge between database and application. It reduces boilerplate code and configurations.

Key classes:

  • JdbcTemplate: Provides simple methods for executing SQL statements and working with data exchange for applications.
  • DataSource: Establish the connection(bridge) of data exchange from database.
  • SimpleJdbcCall: method present in Spring JDBC API, used for interacting with database-stored procedures.

26. Advantages of JdbcTemplate in Spring

  • Reduces boilerplate code: no need to write raw JDBC codes, also bundles common operations.
  • Exception handling: auto handling and conversion of SQLExceptions into Spring's DataAccessException.
  • Prepared statements: Uses prepared statements to prevent SQL injection attacks.
  • Data binding: instead of SQL statements it uses prepared statements, which have better security and performance.

27. Fetching records using Spring JdbcTemplate?

Use the query method of JdbcTemplate with the appropriate SQL query and result extractor.

List<User> users = jdbcTemplate.query("SELECT * FROM users", new BeanPropertyRowMapper<>(User.class));

Spring MVC

28. Spring MVC and its components

A web framework providing MVC architecture to separate presentation, business logic, and data access layers.

Components:

  • DispatcherServlet: Front controller handling requests
  • Model: Data passed between controller and view
  • View: UI layer
  • Controller: Processes requests and returns responses

29. Explain DispatcherServlet and Request Flow in Spring MVC?

It is the central component of the Spring MVC framework and acts as the front controller, receiving all incoming requests and dispatching them to relevant controllers based on the request URL.

Request Flow:

  • The client sends a request to the DispatcherServlet.
  • DispatcherServlet identifies the appropriate controller based on request mapping.
  • The controller processes the request, interacts with the model, and returns a model object.
  • DispatcherServlet selects the appropriate view based on the returned view name.
  • View renders the model data into the final response and sends it back to the client.

2

30. Interceptors

Interceptors are reusable components that intercept request processing and response generation phases in the lifecycle of web applications. They can be used for tasks in which a concern has to be applied globally across multiple controllers like logging, authentication, caching, and authorization.

31. Design Patterns used in Spring MVC

  • MVC Pattern: Separates the application into three layers i.e. presentation, business logic, and data access layers.
  • Front Controller Pattern: Single entry point for all incoming requests, DispatcherServlet receives it and re-directs it to appropriate controllers.
  • Template Method Pattern: View resolvers use templates to render views with consistency in the presentation layer.
  • Strategy Pattern: Different view resolvers can be used based on the required view technology such as InternalResourceViewResolver and ThymeleafViewResolver

32. Explain the most important Spring MVC annotations

  • @Controller: Marks the class as a controller in the Spring MVC framework, It handles and processes all the incoming requests and returns appropriate view or response.
  • @RequestMapping: Used to map a controller method to a specific URL pattern, it can handle various HTTP methods like GET, POST, PUT or DELETE
  • @ModelAttribute: It is used to add an attribute to the model for the view.
  • @RequestParam: Extracts data from the request parameters into method arguments, allowing to access values present in the request URL.
  • @PathVariable: Extracts data from the URL path into method arguments.
  • @SessionAttribute: Used in cases when model attributes are supposed to be stored across multiple requests.

33. Importance of session scope

Session scope plays an important role in maintaining beans for a specific duration which stores crucial information like login credentials, etc.

  • Avoiding Data Repetition
  • Application Security
  • Reduced Database Access

34. How to get ServletConfig and ServletContext objects in Spring Bean?

Use @Autowired annotation to inject them into the bean.

@Autowired
private ServletConfig servletConfig;

@Autowired
private ServletContext servletContext;

35. Data validation

Spring provides various ways to validate data:

  • Bean Validation API: Annotations like @NotNull and @Size can be used to validate bean properties.
  • DataBinder: Binds request parameters to bean properties and performs validation based on annotations.
  • Validator interface: Custom validation logic can be implemented using the Validator interface.

36. Bean Factory vs Application Context

  • Bean Factory: Creates and manages beans.
  • Application Context: Provides additional features like event handling, internationalization, and resource management beyond basic bean management.

37. i18n and localization in Spring MVC

Spring MVC supports i18n and localization, allowing you to develop applications that can be adapted to different languages and cultural contexts.

38. Exception Handling in Spring MVC

Spring MVC provides various mechanisms for handling exceptions:

  • @ExceptionHandler annotation: Defines methods to handle specific exceptions.
  • Global exception handler: Handles all uncaught exceptions.
  • Error pages: Customized error pages can be displayed for different HTTP error codes.

39. ViewResolver class

ViewResolver is responsible for resolving the view name returned by the controller to the actual view implementation.

40. MultipartResolver

MultipartResolver handles file uploads in Spring MVC applications. It parses multipart requests and extracts uploaded files

Spring 5 (Reactive Programming)

41. Spring WebFlux and its types

Spring Webflux is used to develop applications with faster response time and improved scalability. It uses the principles of reactive programming and non-blocking APIs to handle asynchronous requests of data streams.

Types of Spring WebFlux:

  • Functional: Developers use lambdas and streams to create reactive applications.
  • Annotation-based: follows Spring MVC style for configuring controllers, handlers, and filters.

42. What is Spring Reactive Web?

Reactive programming is used for developing high scalability and responsive web applications for handling asynchronous and non-blocking operations efficiently, to provide these features Spring WebFlux provides a sub-framework Spring Reactive Web.

Components:

  • WebClient: Non-blocking HTTP requests
  • Server-Sent Events (SSE): Real-time server-client communication
  • WebSocket: Interactive apps like chat

43. Reactive Streams API

Defines publishers, subscribers, and backpressure handling for asynchronous data streams.

44. Supported Media Types

Spring WebFlux supports various media types for request and response data, including:

  • JSON
  • XML
  • Plain text
  • HTML
  • Multipart/form-data
  • Custom media types

45. Exception handling in Spring Webflux

Spring WebFlux provides various ways to handle exceptions:

  • GlobalExceptionHandler: Handles all uncaught exceptions in the application.
  • WebExceptionHandler: Handles exceptions specific to web requests.
  • ReactiveExceptionHandler: Handles exceptions specific to reactive streams.
Comment

Explore