๐Why Spring Fundamentals Still Decide Interviews
Most Java backend interviews say "Spring Boot", but the questions that separate candidates are about the Spring Framework underneath: how the container creates beans, how proxies make @Transactional work, and why a self-invoked method is not transactional. This list covers those fundamentals. For Boot-specific questions, see Spring Boot interview questions.
Learning Spring from scratch? Start with the Spring Framework tutorial or our live Spring Framework course.
๐Core Container: IoC and DI
1. What is Inversion of Control?
Instead of objects creating their own dependencies, a container creates and wires them. Control over object creation moves from your code to the framework.2. What is dependency injection?
The way Spring implements IoC: dependencies are supplied to an object through its constructor, setters or fields, rather than looked up or created by the object.3. Constructor vs setter vs field injection?
Constructor injection is preferred: dependencies are explicit, fields can be final, the object is never half-built, and it is easy to test. Setter injection suits optional dependencies. Field injection (@Autowired on fields) hides dependencies and makes testing harder.4. BeanFactory vs ApplicationContext?
BeanFactory is the basic container with lazy bean creation. ApplicationContext extends it with eager singleton creation, events, internationalisation, environment and profile support and AOP integration. You almost always use ApplicationContext.5. What is a Spring bean?
An object created, configured and managed by the Spring container.6. How do you define beans?
With stereotype annotations picked up by component scanning (@Component, @Service, @Repository, @Controller), with @Bean methods in a @Configuration class, or (legacy) in XML.7. @Component vs @Service vs @Repository vs @Controller?
All are components.@Repository also translates persistence exceptions into Spring's DataAccessException hierarchy, @Controller marks a web controller, and @Service documents business logic.8. @Configuration vs @Component with @Bean methods?
In a@Configuration class, @Bean methods are proxied, so calling one @Bean method from another returns the same singleton. In a plain @Component ("lite mode") each call creates a new object.9. How does Spring resolve two beans of the same type?
Use@Primary to mark the default, or @Qualifier("name") at the injection point. Otherwise Spring throws NoUniqueBeanDefinitionException.10. How do you handle circular dependencies?
Redesign to remove them โ usually by extracting a third bean. Spring Boot 2.6+ forbids circular references by default;@Lazy on one injection point is a stopgap.๐Bean Scopes and Lifecycle
11. What bean scopes exist?
singleton (default, one per container), prototype (new instance per request), and in web apps request, session, application and websocket.12. What happens when you inject a prototype bean into a singleton?
It is injected once, so the singleton always uses the same instance. UseObjectProvider, a lookup method or a scoped proxy to get a fresh one each time.13. Describe the bean lifecycle.
Instantiate โ populate properties โ Aware callbacks โ BeanPostProcessor before-init โ@PostConstruct / afterPropertiesSet() / init-method โ BeanPostProcessor after-init (where proxies are created) โ in use โ @PreDestroy / destroy() on shutdown.14. What is a BeanPostProcessor?
A hook to modify beans before and after initialisation. Spring uses it for@Autowired processing, AOP proxies and more.15. BeanPostProcessor vs BeanFactoryPostProcessor?
A BeanFactoryPostProcessor modifies bean definitions before any bean is created (for example, property placeholder resolution). A BeanPostProcessor modifies bean instances.16. What are profiles?
Named groups of beans and configuration activated per environment with@Profile("dev") and spring.profiles.active.๐AOP
17. What is AOP?
Aspect-Oriented Programming separates cross-cutting concerns โ logging, security, transactions, metrics โ from business code.18. Explain aspect, advice, join point and pointcut.
An aspect is the module of cross-cutting logic. Advice is the action (@Before, @After, @AfterReturning, @AfterThrowing, @Around). A join point is a point in execution (in Spring, a method call). A pointcut is the expression selecting join points.19. How does Spring AOP work?
With runtime proxies: JDK dynamic proxies for interfaces, CGLIB subclasses otherwise (Spring Boot defaults to CGLIB). Calls go through the proxy, which runs the advice.20. Why does @Transactional not work on a method called from the same class?
Self-invocation bypasses the proxy, so no advice runs. Move the method to another bean, or restructure the call.๐Spring MVC
21. How does a request flow through Spring MVC?
DispatcherServlet โ HandlerMapping finds the controller method โ HandlerAdapter invokes it โ the return value is written by an HttpMessageConverter (for REST) or resolved to a view โ response.22. @Controller vs @RestController?
@RestController is @Controller plus @ResponseBody, so return values are serialised (usually to JSON) instead of resolved as view names.23. @RequestParam vs @PathVariable vs @RequestBody?
Query parameters, URI template segments and the deserialised request body respectively.24. How do you handle exceptions globally?
@RestControllerAdvice with @ExceptionHandler methods, ideally returning RFC 9457 ProblemDetail responses. See the Spring Boot REST API tutorial.25. Filter vs HandlerInterceptor?
Filters are servlet-level and run before DispatcherServlet (security, CORS, logging). Interceptors run inside Spring MVC around handler execution and know which controller method is called.๐Transactions
26. How does @Transactional work?
A proxy opens a transaction before the method, commits on success and rolls back on failure, using the configured PlatformTransactionManager.27. Which exceptions trigger a rollback?
By default only unchecked exceptions (RuntimeException and Error). UserollbackFor = Exception.class to include checked exceptions.28. Explain propagation REQUIRED vs REQUIRES_NEW.
REQUIRED (default) joins an existing transaction or starts one. REQUIRES_NEW suspends the current transaction and starts an independent one โ useful for audit logs that must persist even if the outer transaction rolls back.29. What are isolation levels?
READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE, trading consistency (dirty reads, non-repeatable reads, phantoms) against concurrency.๐Spring Data JPA
30. JpaRepository vs CrudRepository?
CrudRepository provides CRUD; JpaRepository adds paging, sorting, batch operations and JPA-specific methods likeflush().31. What is the N+1 problem and how do you fix it?
Loading a list of entities, then one extra query per entity for a lazy association. Fix withJOIN FETCH, @EntityGraph, batch fetching or DTO projections.32. Lazy vs eager fetching?
Lazy loads associations on first access (default for collections); eager loads them immediately. Prefer lazy and fetch explicitly per use case.๐Spring Security
33. How does Spring Security work?
A chain of servlet filters (the SecurityFilterChain) authenticates the request, stores the Authentication in the SecurityContext and authorises access to URLs and methods.34. How do you configure security in Spring Security 6?
Declare aSecurityFilterChain bean with the lambda DSL; WebSecurityConfigurerAdapter was removed. Full example in our Spring Security JWT tutorial.35. How do you secure individual methods?
Enable@EnableMethodSecurity and use @PreAuthorize("hasRole('ADMIN')") on service methods.๐Keep Going
Review Spring vs Spring Boot, then the Java interview questions and microservices interview questions for full backend rounds.
Prakalpana's Spring Framework course is taught live online with an optional 1-on-1 track, real projects and mock interviews. WhatsApp or call +91 9243078181 for a free demo.