Spring Framework Interview Questions and Answers Overview
This article compiles a comprehensive set of Spring interview questions covering design patterns, core modules, IoC, DI, AOP, proxy mechanisms, bean lifecycle, circular dependency resolution, transaction isolation and propagation, as well as Spring Boot auto‑configuration principles, providing concise explanations for each topic.
Hello everyone, I am Chen. I have gathered a collection of Spring interview questions from the web and organized them here for your reference.
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 proxies.
Singleton Pattern: Spring beans are singleton by default.
Template Method Pattern: Classes ending with *Template (e.g., JdbcTemplate) use this pattern for common database operations.
Wrapper (Decorator) Pattern: Enables dynamic switching of data sources based on client needs.
Observer Pattern: Spring’s event‑driven model follows this pattern.
Adapter Pattern: Spring AOP’s advice uses an adapter to connect enhancements.
2. What are the core modules of Spring?
Spring Core: Provides IoC and DI features.
Spring Context: An enhanced BeanFactory for application contexts.
Spring Web: Supports web application development.
Spring MVC: Implements the MVC pattern for web apps.
Spring DAO: Abstracts JDBC for robust data access.
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 to the Spring framework, which creates and configures beans and resolves their dependencies automatically.
4. What IoC containers does Spring provide and how do they differ?
Spring offers two containers: BeanFactory , which only provides basic bean instantiation and retrieval, and ApplicationContext , a richer subclass of BeanFactory that adds annotation support, internationalization, and other advanced features.
5. Difference between BeanFactory and FactoryBean?
BeanFactory is the IoC container itself. FactoryBean is an interface that allows custom creation of bean instances, typically by wrapping a bean to add extra behavior.
6. Differences among @Repository, @Service, @Component, @Controller?
All four annotations mark a class as a Spring bean, but they indicate the layer to which the bean belongs:
@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 the IoC container supplies required dependencies to a bean at runtime. Spring supports three injection styles: interface injection, constructor injection, and setter injection.
8. What is AOP?
AOP (Aspect‑Oriented Programming) extends OOP by allowing cross‑cutting concerns to be modularized. Spring implements AOP mainly through dynamic proxies (JDK or CGLIB). It defines five advice types: @Before, @AfterReturning, @After, @AfterThrowing, and @Around.
9. Difference between static and dynamic proxies?
Static Proxy: Source code is written or generated beforehand; the proxy class exists as a compiled .class file and typically 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 its methods.
11. Difference between Spring AOP and AspectJ AOP?
Spring AOP is runtime‑based and uses dynamic proxies, while AspectJ AOP is compile‑time (or load‑time) weaving that modifies bytecode directly. AspectJ supports three weaving strategies: compile‑time, post‑compile, and load‑time.
12. Bean lifecycle in Spring
Instantiation – creating the bean instance.
Populate properties – setting bean properties.
Initialization – invoking aware interfaces, BeanPostProcessor callbacks, and custom init‑method.
Destruction – invoking 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. When a circular reference is detected, a bean factory is placed in the third‑level cache, allowing the dependent bean to obtain a reference before full initialization.
14. Why a three‑level cache instead of just a second‑level cache?
The three‑level cache ensures that AOP proxies are created only when necessary, preserving the design principle that proxying occurs after bean initialization rather than immediately after instantiation.
15. Difference between @Autowired and @Resource?
@Resource (Java EE) – resolves by name first (name attribute) then by type; supports both 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 – read uncommitted
READ_COMMITTED – read committed
REPEATABLE_READ – repeatable read
SERIALIZABLE – serializable
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 inside a transaction, otherwise throw exception.
PROPAGATION_NESTED – run in a nested transaction if a current transaction exists.
PROPAGATION_NEVER – must not run within a transaction; throws exception if one exists.
PROPAGATION_REQUIRES_NEW – always start a new transaction, suspending any existing one.
PROPAGATION_NOT_SUPPORTED – execute without a transaction, suspending any existing one.
18. How does Spring Boot auto‑configuration work?
During startup, Spring Boot invokes EnableAutoConfigurationImportSelector.selectImports to obtain a list of candidate configuration classes, reads spring.factories from the spring-boot-autoconfigure JAR to get fully qualified class names, filters them according to the current environment, and finally registers the selected configurations into the IoC container.
Please like, view, and share.
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.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.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.
