Top 20 Spring Core Interview Questions and Answers
Top 20 Spring Core Interview Questions and Answers Work through these 20 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.
How does Spring Environment resolve property precedence?
Debug with --debug or Environment.getPropertySources(). K8s secret as env var overrides application-prod.yml — intentional for secrets. Document precedence for team. Use...
What does @Lazy mean on injection and on @Configuration?
ServiceA needs ServiceB and vice versa — @Lazy on one injection breaks cycle via proxy. Heavy ReportGenerator @Lazy on admin-only path. Prefer refactoring to remove cycles....
How do Spring application events work with @EventListener?
OrderService publishes OrderCreatedEvent after save. EmailListener @EventListener sends confirmation. @TransactionalEventListener(AFTER_COMMIT) prevents email on rolled-back...
Multiple beans of the same type exist and Spring throws an error. How do you fix it?
Spring needs disambiguation when two `@Service` implement the same interface. ```java @Primary @Service public class StripePaymentGateway implements PaymentGateway {} @Service...
How does Spring Boot auto-configuration work?
@SpringBootApplication enables @EnableAutoConfiguration, which loads candidate configs from starters. Each auto-config uses @ConditionalOn* to register beans only when...
What is the N+1 query problem and how do you fix it?
Loading 100 orders then accessing each order's items triggers 101 queries. Fix with JOIN FETCH in JPQL, @EntityGraph on repository method, or a DTO query selecting only...
@RestController vs @Controller?
@Controller is for traditional MVC — return "user/list" view name. @RestController adds @ResponseBody so return types become JSON/XML body — standard for REST APIs. Use...
How does @Transactional work?
Spring creates a proxy around @Transactional beans. Calling createOrder() from another bean starts a transaction on the thread-bound EntityManager; commit on success, rollback...
Explain JWT authentication flow in Spring Security.
POST /login with credentials → auth provider validates → issue JWT with exp/sub/roles. Subsequent requests include Bearer token. JwtAuthenticationFilter runs before...
What are Spring Boot starters?
spring-boot-starter-web brings spring-webmvc, Tomcat, Jackson with tested versions and enables WebMvcAutoConfiguration. You declare one starter instead of 10 coordinates....
How do you handle exceptions globally in Spring?
Create @RestControllerAdvice with @ExceptionHandler(MethodArgumentNotValidException.class) returning 400 + field map, NotFoundException → 404, generic Exception → 500 without...
What is the difference between JPA and Hibernate?
JPA is the API contract; Hibernate is the engine under Spring Boot by default. You write @Entity per JPA spec; Hibernate handles SQL generation. Spring Data JpaRepository adds...
Describe the Spring Bean lifecycle.
Container instantiates the bean, injects dependencies, runs BeanPostProcessors, then @PostConstruct or InitializingBean. Singleton lives for context lifetime; prototype creates...
What is dependency injection and why does Spring use it?
DI supplies collaborators from outside the class — typically via constructor. Problem solved: tight coupling to concrete classes, hard unit tests, scattered object...
Difference between @Component, @Service, @Repository?
All register as singleton beans via component scan. Choose by layer for readability: @Repository on DAOs (plus SQLException translation), @Service on business logic, @Component...
Bean scopes in Spring — singleton vs prototype?
Default singleton shares one instance — stateless services. Prototype for per-operation objects. Request/session scopes tie to web lifecycle with proxies. Prototype inside...
What is the difference between @Configuration and @Component?
AppConfig @Configuration with @Bean DataSource — calling dataSource() from another @Bean method returns same instance. Mistake: @Component Config { @Bean Service s() { return...
BeanFactoryPostProcessor vs BeanPostProcessor — explain both.
BFPP: replace ${placeholder} in definitions via PropertySourcesPlaceholderConfigurer. BPP: @Autowired injection, @PostConstruct detection, AOP proxy wrapping. Custom BPP can...
@Primary vs @Qualifier — when to use each?
Two DataSource beans: @Primary mainDataSource for JPA, @Qualifier("analytics") for reporting repo. Prefer @Qualifier when both are equally valid — @Primary for sensible...
ApplicationContext vs BeanFactory — what extra does ApplicationContext provide?
BeanFactory sufficient for minimal DI tests. ApplicationContext adds @EventListener infrastructure, @MessageSource for i18n, Environment integration. Spring Boot creates...
Spring Core Interview Questions and Answers · Top 50 Spring Core Interview Questions and Answers (2026 Edition) · Top 40 Spring Core Interview Questions | Must-Prepare before interview. · Spring Core Interview Questions Every Fresher Must Know
All interview question lists · Interview guides · Home · Support the platform