Spring Basics
Spring Basics Interview with follow-up questions
1. What is the Spring Framework and why is it widely used?
The Spring Framework is an open-source, lightweight application framework and IoC container for Java/JVM apps. Its foundation is Inversion of Control / Dependency Injection — Spring creates and wires your objects ("beans") so your code stays loosely coupled and testable — plus AOP for cross-cutting concerns and consistent abstractions over data access, transactions, web, messaging, and security.
Why it's so widely used:
- Dependency Injection → loose coupling, easy mocking/testing (constructor injection is the recommended style today).
- AOP → clean handling of logging, security, and declarative transactions (
@Transactional). - Abstractions & integration → consistent support for JPA/Hibernate, JDBC, messaging, REST, etc., with less boilerplate.
- Spring Boot → auto-configuration, starters, and embedded servers make it the default way to build Java services and microservices.
- Huge ecosystem & community → Spring Data, Security, Cloud, Batch.
The 2026 framing that signals current knowledge: modern Spring means Spring Framework 6/7 and Spring Boot 3.x/4.x, which require Java 17+, moved from the javax.* to the jakarta.* namespace (Jakarta EE), and add GraalVM native images (AOT) and virtual-thread support for cheaper concurrency.
Follow-up 1
Can you explain some of the key benefits of using Spring Framework?
Some of the key benefits of using Spring Framework are:
Dependency Injection: Spring Framework promotes the use of dependency injection, which helps in achieving loose coupling between components. This makes the application easier to test, maintain, and modify.
Modularity: Spring Framework follows a modular design approach, allowing developers to pick and choose the components they need for their application. This promotes code reusability and makes the application more maintainable.
AOP Support: Spring Framework provides support for Aspect-Oriented Programming (AOP), which allows developers to modularize cross-cutting concerns such as logging, security, and transaction management. This improves code modularity and maintainability.
Integration: Spring Framework provides seamless integration with other popular frameworks and technologies, such as Hibernate, JPA, and RESTful web services. This makes it easier to build complex enterprise applications.
Community Support: Spring Framework has a large and active community of developers, providing extensive documentation, tutorials, and support.
Follow-up 2
What are some of the core features of Spring Framework?
Some of the core features of Spring Framework are:
Dependency Injection: Spring Framework supports dependency injection, which helps in achieving loose coupling between components. It allows developers to define dependencies between objects and let the framework handle the instantiation and wiring of these objects.
Aspect-Oriented Programming (AOP): Spring Framework provides support for AOP, allowing developers to modularize cross-cutting concerns such as logging, security, and transaction management. AOP enables the separation of concerns and improves code modularity.
Spring MVC: Spring Framework includes a web framework called Spring MVC, which provides a model-view-controller architecture for building web applications. It offers features like request mapping, data binding, and validation.
Spring Data: Spring Framework provides Spring Data, which simplifies the integration with databases and provides a consistent programming model for data access.
Spring Security: Spring Framework includes Spring Security, which provides authentication and authorization capabilities for securing applications.
Follow-up 3
How does Spring Framework support loose coupling?
Spring Framework supports loose coupling through its dependency injection mechanism. Dependency injection allows the components of an application to be loosely coupled by removing the responsibility of creating and managing dependencies from the components themselves.
In Spring Framework, dependencies are defined in a configuration file or through annotations, and the framework is responsible for instantiating and injecting these dependencies into the components that require them. This decouples the components from their dependencies and makes it easier to replace or modify dependencies without affecting the components that use them.
By promoting loose coupling, Spring Framework improves the testability, maintainability, and modularity of applications.
Follow-up 4
What is Inversion of Control (IoC) in the context of Spring Framework?
Inversion of Control (IoC) is a core principle of the Spring Framework. It refers to the process of delegating the control of object creation and dependency injection to an external entity, typically a container or framework.
In the context of Spring Framework, IoC means that the framework is responsible for managing the lifecycle of objects and injecting their dependencies. Instead of objects creating and managing their dependencies themselves, they rely on the framework to provide the necessary dependencies.
This inversion of control allows for loose coupling between components and makes the application easier to test, maintain, and modify. Spring Framework achieves IoC through its dependency injection mechanism, where dependencies are defined in a configuration file or through annotations, and the framework handles the instantiation and injection of these dependencies.
2. What are the different modules in Spring Framework?
Spring is modular — you pull in only what you need. The core groups:
- Core Container —
spring-core,spring-beans,spring-context,spring-expression(SpEL): the IoC container, beans, and DI. - AOP —
spring-aop(and AspectJ integration) for cross-cutting concerns. - Data Access / Integration —
spring-jdbc,spring-orm(JPA/Hibernate),spring-tx(transactions),spring-jms, plusspring-r2dbcfor reactive data. - Web —
spring-web,spring-webmvc(servlet MVC), andspring-webflux(reactive, non-blocking). - Test —
spring-testfor unit/integration testing.
Core Container → AOP → Data Access/Tx → Web (MVC / WebFlux) → Test
Two points worth adding for 2026: this list is the Spring Framework itself — beyond it sit major projects like Spring Boot, Spring Data, Spring Security, Spring Cloud, and Spring Batch, which is what most "Spring" work actually uses. And note spring-webflux (reactive) sits alongside the classic servlet spring-webmvc — the old "Spring DAO" terminology is legacy; today it's JDBC/ORM/@Transactional.
Follow-up 1
Can you explain the role of Spring Core Container?
The Spring Core Container is the foundation of the Spring Framework. It provides the fundamental functionality of the framework, including:
Dependency Injection: The Core Container manages the creation and wiring of objects, allowing dependencies to be injected into objects rather than having them explicitly created and managed by the application code.
Inversion of Control (IoC): The Core Container implements the IoC principle, where the control of object creation and lifecycle is inverted from the application code to the container.
The Core Container consists of the BeanFactory and ApplicationContext interfaces, which provide the necessary functionality for managing objects and their dependencies.
Follow-up 2
What is the purpose of Spring AOP module?
The purpose of the Spring AOP (Aspect-Oriented Programming) module is to provide support for aspect-oriented programming in Spring applications. AOP allows you to modularize cross-cutting concerns, such as logging, transaction management, and security, which are not related to the core business logic of the application.
Spring AOP uses proxy-based AOP, where it creates proxy objects that intercept method invocations and apply advice (additional behavior) before, after, or around the actual method execution. This allows you to add functionality to your application without modifying the original code.
Spring AOP integrates seamlessly with the Spring Core Container, allowing you to apply AOP to Spring-managed beans.
Follow-up 3
How does the Spring Web MVC module work?
The Spring Web MVC module provides the MVC (Model-View-Controller) architecture for building web applications in Spring. Here's how it works:
DispatcherServlet: The central component of Spring Web MVC is the DispatcherServlet, which acts as the front controller. It receives incoming requests and delegates them to the appropriate handler based on the configured mappings.
HandlerMapping: The HandlerMapping interface is responsible for mapping requests to handler methods. It determines which controller and method should handle a particular request.
Controller: Controllers are responsible for processing requests and generating responses. They contain handler methods that are invoked by the DispatcherServlet.
ViewResolver: The ViewResolver interface is responsible for resolving logical view names to actual view implementations. It determines which view should be used to render the response.
Overall, the Spring Web MVC module provides a flexible and powerful framework for building web applications in Spring.
Follow-up 4
What is the role of Spring DAO module?
The Spring DAO (Data Access Object) module provides support for data access operations using Spring's DAO abstraction. Its main role is to simplify the development of data access code and promote good practices.
The key features of the Spring DAO module include:
Exception Translation: The module provides a mechanism for translating low-level data access exceptions into a consistent set of Spring-specific exceptions. This allows you to handle exceptions in a uniform way across different data access technologies.
Template Classes: The module provides template classes, such as JdbcTemplate and HibernateTemplate, which encapsulate common data access operations and handle resource management, exception handling, and transaction management.
Integration with ORM Frameworks: The module integrates seamlessly with popular ORM (Object-Relational Mapping) frameworks like Hibernate and JPA, providing additional features and simplifying the configuration.
Overall, the Spring DAO module helps in achieving a clean separation between the business logic and the data access code, making it easier to develop and maintain data-driven applications.
3. How does Spring Framework handle database operations?
Spring offers layered options for data access, all sharing a consistent exception hierarchy (DataAccessException) and declarative transactions (@Transactional):
- Spring JDBC —
JdbcTemplate(and the newerJdbcClient, a fluent API in Spring 6.1+) removes JDBC boilerplate: connection handling, statement creation, and exception translation. - Spring ORM — integrates JPA/Hibernate so you map objects to tables and let the provider generate SQL.
- Spring Data JPA — the most common choice: define a repository interface (
extends JpaRepository) and Spring generates the implementation, including derived query methods (findByEmail) and@Query. - R2DBC / reactive —
spring-r2dbcand Spring Data R2DBC for non-blocking database access in WebFlux apps. - Declarative transactions — annotate a method
@Transactionaland Spring manages commit/rollback via AOP proxies.
public interface UserRepository extends JpaRepository {
Optional findByEmail(String email); // query derived from the name
}
The framing interviewers want: Spring doesn't replace JDBC/JPA — it abstracts and simplifies them, unifies exception handling, and adds declarative transactions. In practice, most apps use Spring Data JPA on top of Hibernate, with JdbcClient/JdbcTemplate for custom SQL. A current note: prefer constructor-injected repositories and pair with connection pooling (HikariCP, the Boot default).
Follow-up 1
What is Spring JDBC?
Spring JDBC is a module of the Spring Framework that provides a higher-level abstraction for database access using JDBC (Java Database Connectivity). It simplifies the database access code by handling resource management, exception handling, and transaction management. Spring JDBC offers features like PreparedStatement creation, query execution, result set handling, and exception translation. It also provides support for named parameters and stored procedures. With Spring JDBC, you can write database access code that is more concise, readable, and maintainable compared to raw JDBC code.
Follow-up 2
How does Spring Framework simplify the database access code?
Spring Framework simplifies the database access code in several ways:
Dependency Injection: Spring Framework uses dependency injection to manage the dependencies of database access code. It allows you to define and configure database-related beans (e.g., DataSource, JdbcTemplate) and inject them into your application components. This eliminates the need for manual resource management and reduces the complexity of database access code.
Template Classes: Spring Framework provides template classes (e.g., JdbcTemplate, HibernateTemplate) that encapsulate common database operations. These templates handle resource acquisition and release, exception handling, and transaction management, allowing you to focus on the business logic instead of low-level database operations.
Declarative Transaction Management: Spring Framework offers declarative transaction management, which allows you to define transaction boundaries using annotations or XML configuration. This simplifies the management of database transactions and ensures data consistency and integrity without writing boilerplate transaction code.
Object-Relational Mapping (ORM) Integration: Spring Framework integrates with popular ORM frameworks like Hibernate, JPA, and MyBatis. It provides a consistent programming model for database access and simplifies the configuration and management of ORM frameworks. This reduces the amount of boilerplate code required for database operations.
Overall, Spring Framework provides a higher-level abstraction and consistent programming model for database access, making the code more readable, maintainable, and testable.
Follow-up 3
What is Spring Transaction Management?
Spring Transaction Management is a feature of the Spring Framework that provides a declarative approach to manage database transactions. It allows you to define transaction boundaries using annotations or XML configuration, rather than writing low-level transaction management code. Spring Transaction Management simplifies the management of database transactions and ensures data consistency and integrity.
With Spring Transaction Management, you can annotate your service methods or classes with transactional annotations (e.g., @Transactional) to define the transaction boundaries. Spring will automatically manage the transactions, including transaction begin, commit, and rollback, based on the defined annotations.
Spring Transaction Management supports various transaction propagation behaviors, isolation levels, and rollback rules. It integrates with different transaction managers, including JDBC, JTA, and JPA transaction managers. It also provides support for distributed transactions and XA transactions.
By using Spring Transaction Management, you can focus on the business logic of your application and let Spring handle the low-level transaction management, ensuring data consistency and integrity.
Follow-up 4
What is Spring Data Access/Integration?
Spring Data Access/Integration is a feature of the Spring Framework that provides support for various data access and integration technologies. It offers templates, utilities, and abstractions to simplify the integration of these technologies into Spring applications.
Spring Data Access/Integration supports a wide range of data access technologies, including JDBC, JPA, Hibernate, MyBatis, MongoDB, Redis, Cassandra, and more. It provides template classes and utility methods for common database operations, such as querying, updating, and deleting data.
Some of the key features of Spring Data Access/Integration are:
Repository Abstraction: Spring Data Access/Integration provides a repository abstraction that allows you to define repository interfaces with built-in CRUD (Create, Read, Update, Delete) methods. These interfaces can be easily implemented, and Spring will generate the necessary database access code at runtime.
Query Methods: Spring Data Access/Integration supports query methods, which allow you to define queries using method names and parameter names. Spring will automatically generate the corresponding SQL or NoSQL queries based on the method names and parameters.
Pagination and Sorting: Spring Data Access/Integration provides support for pagination and sorting of query results. It allows you to retrieve data in chunks and sort the results based on specific criteria.
Auditing: Spring Data Access/Integration offers auditing support, which allows you to automatically track and manage the creation and modification timestamps of entities.
Overall, Spring Data Access/Integration simplifies the integration of data access technologies into Spring applications, reduces the amount of boilerplate code required for common database operations, and improves the productivity of developers.
4. What is Dependency Injection in Spring Framework?
Dependency Injection (DI) is the pattern where a class receives its dependencies from an external source (Spring's IoC container) instead of constructing them itself — the practical realization of Inversion of Control. The result is loose coupling, easy testing (inject mocks), and clear, declarative wiring.
Spring supports three injection styles, and naming the recommended one matters:
- Constructor injection — the preferred approach: dependencies are
final, guaranteed present, and the class is easy to test without Spring. With a single constructor,@Autowiredis optional. - Setter injection — for optional/reconfigurable dependencies.
- Field injection (
@Autowiredon a field) — concise but discouraged: it hides dependencies, can't befinal, and is hard to test.
@Service
public class OrderService {
private final PaymentClient payments;
public OrderService(PaymentClient payments) { // constructor injection
this.payments = payments;
}
}
The points interviewers reward: Spring resolves beans by type (then by name/@Qualifier to disambiguate, @Primary to pick a default); @Autowired does the wiring; and the modern best practice is constructor injection so dependencies are explicit and immutable. DI is what makes Spring's IoC container the backbone of every app.
Follow-up 1
What are the different types of Dependency Injection supported by Spring?
Spring supports two types of Dependency Injection: constructor injection and setter injection.
Constructor Injection: In constructor injection, the dependencies are provided through the constructor of the class. This ensures that the object is fully initialized before it is used.
Setter Injection: In setter injection, the dependencies are provided through setter methods. This allows for flexibility in changing the dependencies at runtime.
Follow-up 2
What is the difference between constructor injection and setter injection?
The main difference between constructor injection and setter injection is the way dependencies are provided.
Constructor Injection: In constructor injection, the dependencies are provided through the constructor of the class. This ensures that the object is fully initialized before it is used. Constructor injection is preferred when the dependencies are mandatory and should be available at the time of object creation.
Setter Injection: In setter injection, the dependencies are provided through setter methods. This allows for flexibility in changing the dependencies at runtime. Setter injection is preferred when the dependencies are optional or can be changed after the object is created.
Follow-up 3
How does Dependency Injection improve the testability of the code?
Dependency Injection improves the testability of the code in the following ways:
Mocking Dependencies: With Dependency Injection, it becomes easier to mock the dependencies of a class during unit testing. This allows for isolated testing of individual components.
Easy Dependency Replacement: Dependency Injection allows for easy replacement of dependencies with mock objects or stubs. This helps in testing different scenarios without modifying the actual dependencies.
Separation of Concerns: Dependency Injection helps in separating the creation of objects from their usage. This allows for better modularization and testability of the code.
Overall, Dependency Injection promotes the use of interfaces and abstractions, which leads to more modular and testable code.
5. What is Aspect-Oriented Programming (AOP) in Spring Framework?
Aspect-Oriented Programming modularizes cross-cutting concerns — logic needed in many places but unrelated to core business rules, like logging, security, caching, and transactions. Instead of scattering that code through every method, AOP centralizes it in an aspect and applies it declaratively.
The vocabulary to know:
- Aspect — the module bundling the cross-cutting behavior.
- Advice — the action and when it runs:
@Before,@After,@AfterReturning,@AfterThrowing,@Around. - Join point — a point in execution where advice can apply (in Spring AOP, a method execution).
- Pointcut — an expression selecting which join points to advise.
@Aspect @Component
public class LoggingAspect {
@Around("execution(* com.app.service..*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable { /* ... */ return pjp.proceed(); }
}
The mechanics that earn marks: Spring AOP is proxy-based (JDK dynamic proxies or CGLIB), so advice applies to Spring-managed beans on external method calls — a key gotcha is that self-invocation (a bean calling its own method) bypasses the proxy, which is why a @Transactional method called internally won't start a transaction. This proxy mechanism is exactly how Spring implements @Transactional, @Cacheable, and security under the hood.
Follow-up 1
What is a Pointcut in Spring AOP?
In Spring AOP, a pointcut is a predicate that matches join points. It defines the set of join points where an advice should be applied. Pointcuts are used to specify the methods or classes where the aspect's advice should be applied. Spring AOP provides various ways to define pointcuts, such as using regular expressions, annotations, or custom expressions.
Follow-up 2
What are the key components of AOP?
The key components of AOP in Spring Framework are:
Aspect: An aspect is a modular unit of cross-cutting functionality that encapsulates a concern. It is implemented as a Java class.
Join Point: A join point is a specific point in the execution of a program, such as a method invocation or an exception being thrown. In Spring AOP, join points are represented by method invocations.
Pointcut: A pointcut is a predicate that matches join points. It defines the set of join points where an advice should be applied.
Advice: An advice is the action taken by an aspect at a particular join point. It represents the additional behavior that should be executed at a specific pointcut.
Weaving: Weaving is the process of applying aspects to a target object to create a new, woven object. It can be done at compile time, load time, or runtime.
Follow-up 3
What is an Advice in Spring AOP?
In Spring AOP, an advice is the action taken by an aspect at a particular join point. It represents the additional behavior that should be executed at a specific pointcut. There are different types of advice in Spring AOP:
Before advice: Executed before a join point, such as a method invocation.
After returning advice: Executed after a join point completes normally and returns a value.
After throwing advice: Executed after a join point throws an exception.
After advice: Executed after a join point, regardless of its outcome.
Around advice: Wraps around a join point, allowing the aspect to control the join point's execution.
Follow-up 4
How does AOP support separation of concerns?
AOP supports separation of concerns by allowing the modularization of cross-cutting concerns. Cross-cutting concerns are the aspects of a program that affect multiple modules or components, such as logging, security, or transaction management. By using AOP, these concerns can be encapsulated in separate aspects, which can then be applied to the relevant join points in the application's code. This allows for cleaner and more maintainable code, as the main business logic is not cluttered with the implementation of cross-cutting concerns.
Live mock interview
Mock interview: Spring Basics
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.