Top 18 Spring Interview Questions and Answers You Must Know
Explore a comprehensive list of 18 essential Spring interview questions covering design patterns, core modules, IoC, DI, AOP, proxy types, bean lifecycle, circular dependency resolution, transaction management, and Spring Boot auto‑configuration, complete with concise explanations and illustrative diagrams to help you ace your interview.
This article compiles 18 common Spring interview questions and provides concise answers.
1. Which design patterns are used in Spring?
Factory pattern : BeanFactory and ApplicationContext create bean objects.
Proxy pattern : AOP is implemented via JDK dynamic proxies and CGLIB.
Singleton pattern : Spring beans are singleton by default.
Template Method pattern : Classes like JdbcTemplate use this pattern for common database operations.
Wrapper (Decorator) pattern : Enables dynamic switching of data sources.
Observer pattern : Spring’s event‑driven model.
Adapter pattern : Used in Spring AOP’s advice implementation.
2. What are the core modules of Spring?
Spring Core : Provides IoC and DI features.
Spring Context : Enhanced BeanFactory with application context capabilities.
Spring Web : Supports web application development.
Spring MVC : Implements the MVC pattern for web apps.
Spring DAO : Simplifies JDBC coding.
Spring ORM : Integrates popular ORM frameworks such as Hibernate, iBatis, JDO.
Spring AOP : Provides Aspect‑Oriented Programming support.
3. What is IoC?
IoC (Inversion of Control) is a container that manages objects by handing over control of object creation and dependency handling to the Spring framework.
4. What IoC containers does Spring provide and how do they differ?
Spring offers two containers: BeanFactory (basic instantiation and retrieval) and ApplicationContext (extends BeanFactory with annotation support, internationalization, etc.).
5. Difference between BeanFactory and FactoryBean?
BeanFactory is the IoC container itself. FactoryBean is an interface that allows custom creation of bean instances, often used to add proxy logic around a bean.
6. Differences among @Repository, @Service, @Component, @Controller?
All four annotations mark a class as a Spring bean; they mainly help to distinguish layers:
@Repository – DAO layer
@Service – Service layer
@Controller – Web controller layer
@Component – General purpose for classes that do not fit the above categories
7. What is DI?
DI (Dependency Injection) is the mechanism by which Spring supplies required dependencies to a bean at runtime. It is essentially the same concept as IoC from a different perspective.
Spring supports three injection styles: constructor injection, setter injection, and interface (method) injection.
8. What is AOP?
AOP (Aspect‑Oriented Programming) separates cross‑cutting concerns from business logic, reducing coupling and improving reusability. Spring implements AOP mainly via dynamic proxies.
There are two implementation styles:
Static AOP : Weaving occurs at compile time (e.g., AspectJ).
Dynamic AOP : Weaving occurs at runtime using JDK dynamic proxies or CGLIB.
Spring AOP provides five advice types: @Before, @AfterReturning, @AfterThrowing, @After, and @Around.
9. Difference between static and dynamic proxies?
Static proxy : Generated source code compiled beforehand; usually proxies a single class.
Dynamic proxy : Created at runtime via reflection; can proxy multiple implementations of an interface.
10. Difference between JDK dynamic proxy and CGLIB proxy?
JDK dynamic proxy requires the target class to implement an interface and works via reflection. CGLIB creates a subclass of the target class using bytecode manipulation (ASM) and overrides methods.
11. Difference between Spring AOP and AspectJ AOP?
Spring AOP is runtime‑based and uses dynamic proxies. AspectJ AOP is compile‑time (or load‑time) weaving that modifies bytecode directly and supports three weaving modes: compile‑time, post‑compile, and load‑time.
12. What is the Spring bean lifecycle?
Instantiation : Create bean instance.
Populate properties : Set bean properties.
Initialization : Call aware interfaces, BeanPostProcessor before/after methods, and custom init‑method.
Destruction : Call DisposableBean destroy method and custom destroy‑method.
13. How does Spring resolve circular dependencies?
Spring uses a three‑level cache (singleton, early‑reference, and factory) to separate instantiation from initialization, allowing beans to reference each other before full initialization.
14. Why use a three‑level cache instead of a two‑level cache?
The three‑level cache ensures that AOP proxies are created only when a true circular dependency is detected, preserving Spring’s design principle of performing proxy creation after bean initialization.
15. Difference between @Autowired and @Resource?
@Resource (Java EE) – resolves by name first, then by type; supports name and type attributes.
@Autowired (Spring) – resolves solely by type; name matching requires @Qualifier or @Primary.
16. Spring transaction isolation levels?
DEFAULT – Database default.
READ_UNCOMMITTED – Allows dirty reads.
READ_COMMITTED – Prevents dirty reads.
REPEATABLE_READ – Prevents non‑repeatable reads.
SERIALIZABLE – Highest isolation, prevents phantom reads.
17. Spring transaction propagation behaviors?
PROPAGATION_REQUIRED – Join existing transaction or start a new one.
PROPAGATION_SUPPORTS – Execute within a transaction if present.
PROPAGATION_MANDATORY – Must run within a transaction; otherwise throws exception.
PROPAGATION_NESTED – Executes within a nested transaction if a current transaction exists.
PROPAGATION_NEVER – Must not run within a transaction; throws exception if one exists.
PROPAGATION_REQUIRES_NEW – Suspends existing transaction and starts a new one.
PROPAGATION_NOT_SUPPORTED – Suspends any existing transaction and runs non‑transactionally.
18. How does Spring Boot auto‑configuration work?
During startup, EnableAutoConfigurationImportSelector.selectImports gathers a list of candidate auto‑configuration classes.
Spring reads spring.factories from spring-boot-autoconfigure.jar to obtain fully qualified class names.
Filters remove configurations that do not meet the current environment conditions.
The remaining configurations are applied to the EnableConfigurationProperties bean and registered in the IoC container.
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.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
