Spring Framework and Spring Boot are essential topics for Java backend interviews at all levels.

IoC and DI

Q: What is IoC (Inversion of Control)?

Instead of objects creating their dependencies, the container injects them:

  // Without IoC
public class OrderService {
    private PaymentService payment = new StripePaymentService();  // tight coupling
}

// With IoC
@Service
public class OrderService {
    private final PaymentService payment;
    public OrderService(PaymentService payment) {  // injected by Spring
        this.payment = payment;
    }
}
  

Q: @Autowired vs @Inject vs @Resource?

@Autowired @Inject (JSR-330) @Resource (JSR-250)
Source Spring Standard Java Standard Java
Matching By type, then name By type By name, then type
Required Optional (default true) Always required Always required

Prefer constructor injection:

  @Service
public class OrderService {
    private final OrderRepository repo;
    private final PaymentService payment;

    public OrderService(OrderRepository repo, PaymentService payment) {
        this.repo = repo;
        this.payment = payment;
    }
}
  

Bean Lifecycle

Q: Spring Bean lifecycle?

  Instantiate → Populate properties → BeanNameAware → BeanFactoryAware
→ @PostConstruct → InitializingBean.afterPropertiesSet()
→ Custom init-method → Bean ready → @PreDestroy → DisposableBean.destroy()
→ Custom destroy-method
  

Q: @Component vs @Service vs @Repository vs @Controller?

All are @Component stereotypes. Differences are semantic:

Annotation Purpose
@Component Generic Spring bean
@Service Business logic layer
@Repository Data access (adds exception translation)
@Controller Web layer (MVC)
@RestController REST API (@Controller + @ResponseBody)

AOP

Q: How does Spring AOP work?

Spring AOP uses proxy-based weaving:

  Client → Proxy → @Before advice → Target method → @After advice → Client
  

Two proxy types:

  • JDK Dynamic Proxy — if target implements interface(s)
  • CGLIB Proxy — subclasses the target (no interface needed)

Q: Common AOP use cases?

  • Logging and auditing
  • Transaction management (@Transactional)
  • Security checks (@PreAuthorize)
  • Performance monitoring
  • Exception handling

Q: @Transactional — propagation and isolation?

Propagation:

Value Behavior
REQUIRED (default) Join existing or create new
REQUIRES_NEW Always create new, suspend current
NESTED Nested transaction with savepoint
NOT_SUPPORTED Run without transaction

Common pitfall — self-invocation bypasses proxy:

  @Service
public class OrderService {
    public void createOrder() {
        saveOrder();  // @Transactional NOT applied — no proxy!
    }

    @Transactional
    public void saveOrder() { /* ... */ }
}
  

Fix: inject self or extract to separate service.

Spring Boot

Q: How does auto-configuration work?

  @SpringBootApplication
  → @EnableAutoConfiguration
    → Loads META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
      → Each @Configuration class has @ConditionalOn* annotations
        → Only activates if conditions met
  

Example:

  @Configuration
@ConditionalOnClass(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.url")
public class DataSourceAutoConfiguration { /* ... */ }
  

Q: Spring Boot vs Spring Framework?

Spring Framework Spring Boot
Configuration Manual (XML/Java) Convention over configuration
Server Deploy WAR to Tomcat Embedded server
Dependencies Manual version management Starter POMs
Production features Add manually Actuator built-in

Common Scenario Questions

Q: How to handle circular dependency?

  1. Redesign — extract shared logic to third bean (best)
  2. @Lazy — delay initialization of one bean
  3. Setter injection — instead of constructor (not recommended)

Constructor injection detects circular dependencies at startup (fail fast).

Q: Difference between Filter, Interceptor, and AOP?

Filter Interceptor AOP
Scope Servlet level Spring MVC Spring beans
Runs before DispatcherServlet Controller Any Spring bean method
Use for Encoding, auth Logging, auth Transactions, logging