Backend · Fresher-relevant
Spring Boot interview questions covering auto-configuration, REST controllers, JPA/Hibernate, and Spring Security.
What is dependency injection and how does Spring implement it?
Tip: DI is providing a class's dependencies from outside rather than constructing them internally, enabling loose coupling and testability. Spring's IoC container creates and wires beans; prefer constructor injection (immutability, easier testing, no field reflection) over field injection.
How does Spring Boot auto-configuration work?
Tip: @EnableAutoConfiguration (via @SpringBootApplication) scans the classpath and conditionally configures beans using @ConditionalOnClass / @ConditionalOnMissingBean. Starters pull curated dependencies; auto-config backs off whenever you define your own bean, so your configuration always wins.
What is the difference between @Component, @Service, @Repository, and @Controller?
Tip: All are stereotype annotations that register beans via component scanning. @Service marks business logic, @Repository marks the persistence layer (and translates DB exceptions into DataAccessException), @Controller/@RestController marks web endpoints. They are semantically distinct but technically specialisations of @Component.
Explain the difference between @RequestParam, @PathVariable, and @RequestBody.
Tip: @PathVariable binds a URL template segment (/users/{id}). @RequestParam binds a query string or form parameter (?page=2). @RequestBody deserialises the request body (usually JSON) into an object via Jackson. Use @Valid with @RequestBody to trigger bean validation.
What are bean scopes in Spring?
Tip: Default is singleton (one instance per container). prototype creates a new instance per injection. Web scopes include request, session, and application. Be careful injecting a shorter-lived scope into a singleton — use a proxy or ObjectProvider to avoid a stale reference.
How does transaction management work with @Transactional?
Tip: @Transactional wraps a method in a transaction via an AOP proxy: commit on success, rollback on a RuntimeException by default (checked exceptions need rollbackFor). Because it is proxy-based, self-invocation within the same class bypasses it, and the method must be public. Tune propagation (REQUIRED, REQUIRES_NEW) and isolation as needed.
What is the N+1 query problem in JPA/Hibernate and how do you fix it?
Tip: Lazy-loading a collection for N parent rows fires 1 query for the parents plus N queries for children. Fix with a JOIN FETCH / @EntityGraph to load eagerly in one query, or batch fetching (hibernate.default_batch_fetch_size). Detect it by logging SQL in dev.
How do you secure REST endpoints in Spring Boot?
Tip: Use Spring Security with a SecurityFilterChain: stateless JWT bearer tokens for APIs (no server session), method-level @PreAuthorize for role checks, BCrypt for password hashing, and HTTPS enforced. Disable CSRF for stateless token APIs but keep it for cookie-based sessions.
InterviewEra generates role-specific questions using your actual projects and skills. Get scored feedback on technical depth, clarity, and structure — free to start.