Lecture 37: Revision & Hands-on (Spring Core)

BMC201 - Web Technology

Mr. Prashant Kumar Nag

2026-03-25

Lecture 37

Revision & Hands-on (Spring Core)

Week 12 | Unit V: REST Services
BMC201 - Web Technology
Mr. Prashant Kumar Nag, Assistant Professor

Session Goals


  • Revise all essential Spring Core concepts from Unit IV
  • Connect IoC, DI, AOP, and bean lifecycle in one flow
  • Build a mini layered app (controller -> service -> repository)
  • Practice debugging common Spring configuration mistakes
  • Prepare for end-sem practical and viva questions

Spring Core Revision Map


flowchart LR
  A[IoC Container] --> B[Dependency Injection]
  B --> C[Bean Scopes]
  C --> D[Bean Lifecycle]
  D --> E[AOP]
  E --> F[Layered Architecture]
  F --> G[Hands-on Mini App]

Think in terms of responsibilities: container manages, beans execute, aspects monitor.

IoC and DI Recap


IoC: Spring controls object creation and wiring.
DI: Dependencies are provided to classes from outside.

Injection Type Pros Caution
Constructor Clear dependencies, testable, immutable Large constructors can become verbose
Setter Optional dependency support Object can be partially configured
Field Minimal code Hard to test, hidden dependency

Mini App Setup


@SpringBootApplication
public class DemoApp {
  public static void main(String[] args) {
    SpringApplication.run(DemoApp.class, args);
  }
}

@RestController
@RequestMapping("/api/tasks")
public class TaskController {
  private final TaskService service;
  public TaskController(TaskService service) { this.service = service; }
}

Layered Architecture Recap


flowchart TD
  A[Controller] --> B[Service]
  B --> C[Repository]
  C --> D[Database]

  • Controller: request/response handling
  • Service: business rules
  • Repository: data access
  • Keep logic out of controllers for maintainability

Bean Scope Choice in Practice


Choose scope by state behavior:

  • singleton: stateless services (TaskService, validators)
  • prototype: short-lived object with independent state
  • request: per-request context, tracing info
  • session: shopping cart, logged-in preferences

Note

Avoid mutable shared state in singleton beans.

Lifecycle Hook Recap


@Component
public class CacheWarmup {

  @PostConstruct
  public void init() {
    System.out.println("Cache initialized");
  }

  @PreDestroy
  public void shutdown() {
    System.out.println("Cache shutdown");
  }
}

AOP Logging Aspect


@Aspect
@Component
public class TimingAspect {
  @Around("execution(* com.example.service.*.*(..))")
  public Object profile(ProceedingJoinPoint pjp) throws Throwable {
    long start = System.currentTimeMillis();
    Object result = pjp.proceed();
    System.out.println(pjp.getSignature() + " took " +
      (System.currentTimeMillis() - start) + "ms");
    return result;
  }
}

Common Debug Checklist


  • Bean not found: check @ComponentScan package boundaries
  • Ambiguous bean: use @Qualifier or @Primary
  • AOP not applied: verify proxy creation (@EnableAspectJAutoProxy in non-Boot)
  • @PreDestroy not called: ensure context closes gracefully
  • Circular dependency: refactor to constructor + interface separation

Hands-on Task


Build a mini TODO module:

  1. Add TaskController, TaskService, TaskRepository
  2. Use constructor injection in all classes
  3. Add one AOP timing aspect for service methods
  4. Add one bean with @PostConstruct/@PreDestroy
  5. Test endpoint /api/tasks

Exam-Oriented Questions


Prepare:

  1. Explain IoC and DI with examples
  2. Compare constructor and setter injection
  3. Explain bean lifecycle and callbacks
  4. Draw layered architecture for Spring app
  5. Explain practical use of AOP in one real scenario

Summary


  • Spring Core concepts are interconnected, not isolated
  • Unit IV mastery makes Unit V implementation easier
  • Strong architecture and clean DI improve marks and maintainability
  • Hands-on coding is the best revision strategy before exam

Questions?

Next: Lecture 38 - Spring Boot Configuration, Annotations & Build Systems