Spring Interview Question and Answers

1) What is Spring Framework (Spring 6)?

Spring Framework is a comprehensive programming and configuration model for Java-based enterprise applications. As of Spring 6, it supports:

  • Java 17+ as a baseline

  • Jakarta EE 9 (e.g., jakarta.servlet instead of javax.servlet)

  • Native support for GraalVM and AOT (Ahead-of-Time) compilation

  • Enhanced observability and performance


2) What are the advantages of Spring Framework 6?

  • Loose Coupling & Dependency Injection (DI)

  • Predefined Templates (JdbcTemplate, RestTemplate, etc.)

  • Faster Development

  • Declarative Programming (with annotations)

  • Lightweight & Modular

  • Native Compilation Support (via GraalVM)

  • Better Observability (Micrometer support)

  • Improved Testing Support


3) What are the modules in Spring 6?

  • Core Container: Beans, Core, Context, SpEL

  • Data Access/Integration: JDBC, ORM, Transactions, JMS

  • Web: Spring MVC, WebFlux (reactive)

  • AOP, Aspects and Instrumentation

  • Test: JUnit 5, Mockito, Spring TestContext

  • Native: GraalVM and AOT support


4) What is IoC and DI?

IoC (Inversion of Control) is a design principle where control of object creation is inverted to a container (Spring).
DI (Dependency Injection) is how Spring implements IoC.

Without DI:

class Employee {
Address address = new Address(); // tightly coupled }

With DI:

class Employee {
Address address; public Employee(Address address) { this.address = address; // loosely coupled } }

5) What is the role of IoC Container in Spring?

The IoC container is responsible for:

  • Creating objects

  • Configuring them

  • Wiring dependencies

  • Managing lifecycle


6) Types of IoC Containers in Spring:

  • BeanFactory: Basic container

  • ApplicationContext: More feature-rich, supports internationalization, AOP, etc.


7) Difference: BeanFactory vs ApplicationContext

FeatureBeanFactoryApplicationContext
Lazy InitializationYesNo (by default)
AOP SupportNoYes
i18n SupportNoYes
Event PropagationNoYes
Annotation ConfigLimitedFull support

8) Constructor vs Setter Injection

CriteriaConstructor InjectionSetter Injection
Partial InjectionNot possiblePossible
Overriding ValuesNoYes
Immutable DependencyYesNo
Use CaseMandatory dependenciesOptional dependencies

9) Autowiring in Spring

Autowiring automatically injects beans without manual configuration.

Modes:

ModeDescription
noDefault, no autowiring
byNameMatches bean by property name
byTypeMatches bean by type
constructorInjects using constructor

Note: XML-based autowiring is deprecated in favor of @Autowired, @Inject, @Qualifier.


10) Bean Scopes in Spring 6

ScopeDescription
singletonSingle shared instance (default)
prototypeNew instance per request
requestOne per HTTP request (web only)
sessionOne per HTTP session
applicationOne per ServletContext
websocketOne per WebSocket

11) What is the default bean scope?

The singleton scope is the default.


12) Are Singleton Beans Thread-Safe?

No, Spring singleton beans are not thread-safe by default. You must handle synchronization explicitly.


13) What is Dependency Injection (DI)?

DI is a design pattern that promotes loose coupling by injecting dependencies into objects rather than letting objects construct their dependencies.


14) What is a ViewResolver in Spring MVC?

A ViewResolver maps logical view names returned by controllers to actual views (e.g., JSPs, Thymeleaf, etc.).


15) View Resolver Pattern

  • Controller returns view name (e.g., "home")

  • ViewResolver maps it to an actual view (/WEB-INF/views/home.jsp or home.html)

  • Supports pluggable view technologies (JSP, Thymeleaf, PDF, Excel)


16) Spring Transaction Management Types

  1. Programmatic – using TransactionTemplate or PlatformTransactionManager

  2. Declarative – using @Transactional

Declarative is recommended in most cases.


17) Advantages of JdbcTemplate

  • Reduces boilerplate code (no need to handle connection, statement, result set manually)

  • Handles exceptions and resources automatically


18) Key Classes in Spring JDBC API

  • JdbcTemplate

  • NamedParameterJdbcTemplate

  • SimpleJdbcInsert

  • SimpleJdbcCall


19) How to fetch records using JdbcTemplate

Use query() method with:

  • RowMapper: For row-wise mapping

  • ResultSetExtractor: For custom extraction logic


20) Advantage of NamedParameterJdbcTemplate

  1. Improves readability using named parameters instead of ?, e.g.:

String sql = "SELECT * FROM users WHERE name = :name";

21) What is the front controller in Spring MVC?

DispatcherServlet acts as the front controller that routes requests to appropriate controllers and views.


22) What is AOP?

Aspect-Oriented Programming is a way to modularize cross-cutting concerns like logging, security, and transactions into reusable modules called aspects.


23) Advantages of Spring AOP

  • Modularizes repetitive concerns

  • Reduces code duplication

  • Dynamic proxy-based implementation


24) Key AOP Terminology

TermDescription
JoinPoint                        A point during execution (e.g., method call)
Advice                        Action at a JoinPoint (e.g., before, after)
Pointcut                        Expression to select JoinPoints
Aspect                        Class with Advices and Pointcuts
Target                        Actual object being advised
Proxy                        Wrapper created to apply AOP
Weaving                        Applying aspects to target objects


Comments