Top 70 Spring Interview Questions Every Java Developer Should Know
This comprehensive guide lists and explains over seventy common Spring framework interview questions, covering core concepts, modules, IoC, AOP, bean scopes, configuration styles, MVC, data access, transaction management, and annotation-based programming to help Java developers prepare effectively.
Spring Framework Overview
Spring is an open‑source Java EE development framework that provides a lightweight container, IoC, AOP, MVC, transaction management, and integration with various data access technologies.
Key Advantages
Lightweight (≈2 MB core)
Inversion of Control (IoC) for loose coupling
Aspect‑Oriented Programming (AOP) support
Comprehensive container managing bean lifecycle
Well‑designed MVC web framework
Unified transaction management across JTA, JDBC, Hibernate, JPA, etc.
Consistent exception translation
Core Modules
Core
Bean
Context
Expression Language
JDBC
ORM
OXM
JMS
Transaction
Web, Web‑Servlet, Web‑Struts, Web‑Portlet
Important Containers
The BeanFactory is the fundamental container; ApplicationContext builds on it, adding resource loading, message resolution, and event publication.
BeanFactory Implementations
The most common implementation is org.springframework.beans.factory.xml.XmlBeanFactory, which reads bean definitions from XML files.
Dependency Injection (DI)
DI separates object creation from usage. Two main injection types are constructor injection (preferred for mandatory dependencies) and setter injection (useful for optional dependencies).
Bean Scopes
singleton : one instance per container (default)
prototype : a new instance each request
request : one instance per HTTP request (WebApplicationContext only)
session : one instance per HTTP session (WebApplicationContext only)
globalSession : one instance per global HTTP session (WebApplicationContext only)
Bean Lifecycle
Container reads bean definitions and instantiates beans.
Properties are set.
BeanNameAware and BeanFactoryAware callbacks are invoked if implemented.
BeanPostProcessor postProcessBeforeInitialization runs.
InitializingBean afterPropertiesSet or custom init‑method is called.
BeanPostProcessor postProcessAfterInitialization runs.
If DisposableBean is implemented, destroy is called on shutdown.
Injection of Collections
<list>– ordered collection allowing duplicates. <set> – unordered collection without duplicates. <map> – key‑value pairs of any type. <props> – string‑based properties.
Annotation‑Based Configuration
Java‑based configuration replaces XML using annotations such as: @Configuration – marks a class as a source of bean definitions. @Bean – declares a method that returns a bean. @Component, @Service, @Repository, @Controller – stereotype annotations for component scanning. @Autowired – injects dependencies by type (constructor, setter, field). @Qualifier – disambiguates when multiple beans of the same type exist. @Required – enforces mandatory property injection. @PostConstruct and @PreDestroy – lifecycle callbacks.
Data Access Support
Spring simplifies JDBC via JdbcTemplate, providing resource management, exception translation, and convenient query execution.
DAO support integrates with JDBC, Hibernate, JPA, JDO, iBatis, etc., allowing seamless switching between persistence technologies.
Transaction Management
Programmatic : manage transactions via code (flexible but harder to maintain).
Declarative : use @Transactional or XML to separate transaction logic from business code (preferred for most cases).
Aspect‑Oriented Programming (AOP)
AOP modularizes cross‑cutting concerns such as logging, security, and transaction management.
Aspect : class annotated with @Aspect containing advice and pointcuts.
Advice types : before, after, after‑returning, after‑throwing, around.
Pointcut : expression that selects join points.
Join point : a specific method execution point.
Introduction : adds new methods or fields to existing classes.
Proxy : runtime object that applies advice to the target.
Spring MVC
Spring MVC follows the DispatcherServlet front controller pattern, handling all HTTP requests and delegating to controllers. @Controller marks a class as a controller. @RequestMapping maps URLs to controller classes or handler methods.
WebApplicationContext extends ApplicationContext with web‑specific features.
The framework supports both XML‑based and annotation‑based configuration for controllers, view resolvers, and handler mappings.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Baidu Tech Salon
Baidu Tech Salon, organized by Baidu's Technology Management Department, is a monthly offline event that shares cutting‑edge tech trends from Baidu and the industry, providing a free platform for mid‑to‑senior engineers to exchange ideas.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
