Spring Spring Boot
29 interview questions and answers in the Spring Spring Boot section.
Questions in this section
- What is Dependency Injection?
- Difference between Constructor, Setter and Field Injection
- Which Injection Type is Recommended and Why?
- What is a Bean in Spring?
- How to Create a Bean in Spring?
- What is Bean Lifecycle?
- What are the Bean Lifecycle Stages?
- What is BeanPostProcessor?
- What do @PostConstruct and @PreDestroy Methods Do?
- What is Bean Scope?
- What Scopes Exist in Spring?
- Difference between Singleton and Prototype Scope?
- What is a Proxy in Spring?
- When Does Spring Create a Proxy?
- What is AOP (Aspect-Oriented Programming)?
- What is Aspect, Advice, Pointcut, Join Point?
- What Does the @Transactional Annotation Do?
- Why @Transactional Does Not Work with Self-Invocation?
- How to Solve the Self-Invocation Problem?
- What is Auto-Configuration in Spring Boot?
- How @SpringBootApplication Works
- What is a Starter in Spring Boot?
- What Does the @ComponentScan Annotation Do?
- What is a @Configuration Class?
- Difference between @Component, @Service, @Repository, @Controller?
- What Does the @Autowired Annotation Do?
- What to Do If There Are Multiple Beans of the Same Type?
- What is @Qualifier?
- What are Profiles in Spring?
Study navigator
29 questions for Middle Java Developer interview preparation.
All Questions
Topic Dependency Map
+------------------------------------------+
| DI FUNDAMENTALS (1-3) |
| 1. Dependency Injection |
| 2. Constructor vs Setter vs Field |
| 3. Best practices |
+------------------|-----------------------+
|
+--------------------------|--------------------------+
v v v
+---------------+ +---------------+ +--------------------+
| BEAN CONCEPT | | LIFECYCLE | | SCOPE |
| (4-5) | | (6-9) | | (10-12) |
| 4. Bean | | 6. Lifecycle | | 10. Scope concept |
| 5. Creation | | 7. 12 steps | | 11. Scope types |
| | | 8. BPP | | 12. Singleton vs |
| | | 9. @PostCon/ | | Prototype |
| | | @PreDest | | |
+-------+-------+ +-------+-------+ +--------+-----------+
| | |
+------------------------|------------------------+
v
+------------------------------------------+
| PROXY & AOP (13-19) |
| 13. Proxy (concept) |
| 14. When Spring creates proxy |
| 15. AOP |
| 16. Aspect, advice, pointcut |
| 17. @Transactional |
| 18-19. Self-invocation problem |
+------------------------------------------+
|
+------------------------|------------------------+
v v v
+---------------+ +---------------+ +--------------------+
| SPRING BOOT | | ANNOTATIONS | | |
| (20-24) | | (25-29) | | |
| 20. AutoConfig | | 25. @Component | | |
| 21. @SpringBoot| | @Service... | | |
| 22. Starters | | 26. @Autowired | | |
| 23. @CompScan | | 27. Multiple | | |
| 24. @Config | | beans | | |
| | | 28. @Qualifier | | |
| | | 29. Profiles | | |
+---------------+ +---------------+ +--------------------+
Recommended Study Order
Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | DI basics | Q1, Q2, Q3 | What is DI, injection types, recommendations |
| 2 | Bean concept | Q4, Q5 | Bean vs POJO, creation methods |
| 3 | Scope | Q10, Q12 | Singleton vs Prototype |
| 4 | Annotations | Q25, Q26 | @Component vs @Service, @Autowired |
| 5 | Spring Boot basics | Q20, Q22 | Auto-configuration, starters |
Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Lifecycle | Q6, Q7, Q9 | Stages, @PostConstruct/@PreDestroy |
| 2 | BPP | Q8 | BeanPostProcessor, proxying |
| 3 | Scope types | Q11 | Request, Session, Application scope |
| 4 | Proxy | Q13, Q14 | CGLIB vs JDK Proxy, when created |
| 5 | @Transactional | Q17, Q18, Q19 | How it works, self-invocation |
| 6 | @SpringBootApplication | Q21 | Composite annotations, proxyBeanMethods |
| 7 | Bean resolution | Q27, Q28, Q29 | @Qualifier, @Primary, Profiles |
Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | Lifecycle deep | Q7 (Senior) | BPP ordering, early init, circular deps |
| 2 | AOP internals | Q15, Q16 | Spring AOP vs AspectJ, advice types, pointcut matching |
| 3 | @Transactional deep | Q17 (Senior) | Propagation, isolation, rollback rules, ConnectionReleaseMode |
| 4 | Autoconfiguration | Q20, Q23 | Conditional annotations, spring.factories vs .imports, @ComponentScan internals |
| 5 | @Configuration internals | Q24 | Full vs Lite mode, CGLIB proxy, @Bean inter-dependencies |
| 6 | Bean creation | Q5 (Senior) | ImportSelector, ImportBeanDefinitionRegistrar, FactoryBean, GraalVM |
| 7 | Proxy & self-invocation | Q14, Q18, Q19 | All 4 solutions, trade-offs, AopContext |
Key Topic Connections
Topic: DI and Bean
Q1 (DI) -> Q2 (Injection types) -> Q3 (Recommendations)
|
Q4 (Bean) -> Q5 (Creation) -> Q25 (@Component vs @Service...)
Key connections:
- Q1 <-> Q4: DI is the principle, Bean is the object managed by the container
- Q2 <-> Q3: Constructor injection is recommended, but there are exceptions
- Q4 <-> Q5: Bean = concept, @Component/@Bean = creation methods
- Q5 <-> Q23: @ComponentScan = how Spring finds @Component classes
Topic: Lifecycle and Scope
Q6 (Lifecycle) -> Q7 (12 steps) -> Q8 (BPP) -> Q9 (@PostConstruct)
|
Q10 (Scope) -> Q11 (Scope types) -> Q12 (Singleton vs Prototype)
Key connections:
- Q6 <-> Q8: BPP is a key lifecycle mechanism (proxying, initialization)
- Q7 <-> Q8: BPPs are called at multiple lifecycle stages
- Q9 <-> Q17: @PostConstruct does NOT work with @Transactional (proxy does not exist yet!)
- Q10 <-> Q13: Request/Session scopes create proxies for injection into singleton
Topic: Proxy and AOP
Q13 (Proxy) -> Q14 (When created) -> Q15 (AOP) -> Q16 (Advice/Pointcut)
| |
Q17 (@Transactional) -> Q18 (Self-invocation) -> Q19 (Solutions)
Key connections:
- Q13 <-> Q14: Proxy is the mechanism, Q14 explains WHEN Spring creates it
- Q15 <-> Q17: @Transactional is the most common AOP example
- Q17 <-> Q18: Self-invocation is the main @Transactional trap
- Q18 <-> Q19: 4 solutions: refactoring, self-injection, TransactionTemplate, AopContext
Topic: Spring Boot and Annotations
Q20 (AutoConfig) -> Q21 (@SpringBootApplication) -> Q22 (Starters)
|
Q23 (@ComponentScan) -> Q24 (@Configuration) -> Q25 (@Component/@Service/...)
|
Q26 (@Autowired) -> Q27 (Multiple beans) -> Q28 (@Qualifier) -> Q29 (Profiles)
Key connections:
- Q20 <-> Q21: @SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration
- Q21 <-> Q24: @SpringBootConfiguration = @Configuration with proxyBeanMethods
- Q23 <-> Q5: @ComponentScan -> ASM scanning -> BeanDefinition creation
- Q26 <-> Q27-Q28: @Autowired -> multiple beans -> @Qualifier/@Primary
Cheat Sheet: What to Know for Each Level
Junior
- DI = dependencies are provided from outside, not via new
- Constructor injection is recommended, Field injection is not
- Bean = object managed by Spring, POJO = regular object
- @Component, @Service, @Repository, @Controller = stereotype annotations
- @Autowired = dependency injection by type
- Singleton = one per entire context, Prototype = new on each request
- Spring Boot = auto-configuration + starters + embedded server
Middle
- Bean Lifecycle: Instantiation -> Populate -> Initialize -> Ready -> Destroy
- @PostConstruct does NOT work with @Transactional (proxy does not exist yet)
- BPP = mechanism for modifying beans; @Transactional, @Async work through it
- CGLIB (subclass) vs JDK Proxy (by interface)
- @Transactional: rollback only for RuntimeException + Error by default
- Self-invocation: calling a @Transactional method from the same class does NOT work
- @Primary = default bean, @Qualifier = explicit choice
- Profiles = different configuration for dev/test/prod
Senior
- 3-level cache for circular dependencies: singletonObjects, earlySingletonObjects, singletonFactories
- Full mode (@Configuration) = CGLIB proxy, intercepts @Bean calls -> singleton. Lite mode = no proxy, each call = new instance
- AOP: Spring AOP (runtime proxy, methods only) vs AspectJ (compile/load-time weaving, any join point)
- Autoconfiguration: conditional (@ConditionalOnClass, @ConditionalOnMissingBean), spring.factories -> .imports (Boot 2.7+)
- @Autowired resolution: byType -> @Qualifier -> @Primary -> byName
- proxyBeanMethods = false: faster startup, but @Bean inter-calls create new instances
- Transaction propagation: REQUIRED (default), REQUIRES_NEW, NESTED, etc.
File Format
Each file contains:
- Junior Level - basic understanding, simple analogies, examples
- Middle Level - internals, common pitfalls, practical examples
- Senior Level - deep dive, edge cases, production experience, monitoring
- Interview Cheat Sheet - key points, common questions, red flags, related topics