Unit IV Block 2: Spring Patterns and Configuration
Lectures 29-32 - design patterns, AOP, and configuration styles
Block Overview
This block deepens Unit IV by showing how Spring applies classic design patterns and how cross-cutting concerns and bean configuration are handled in real projects.
Course Outcome: CO-4 (K4 - Analysis)
Lectures Covered: 29-32
Theme: Patterns, AOP, and configuration
Lecture 29: Introduction to Design Patterns in Spring
What is Spring?
- Lightweight Java application framework for enterprise apps
- Provides IoC container, DI, AOP, and MVC
- Removes tight coupling between classes
- Large ecosystem: Spring Core, Boot, Data, Security, Cloud
Inversion of Control (IoC)
- Framework controls object creation — not your code
- Spring IoC container reads configuration and wires beans
- Key container:
ApplicationContext
Dependency Injection Types
| Type | How | When to Use |
|---|---|---|
| Constructor | @Autowired on constructor |
Preferred — explicit, immutable |
| Setter | @Autowired on setter method |
Optional dependencies |
| Field | @Autowired on field |
Avoid — not testable |
Spring Configuration Styles
- XML:
<bean>declarations in XML — legacy projects - Java Config:
@Configuration+@Bean— full control - Annotations:
@Component+@Autowired— modern projects
Lecture 30: Factory Design Pattern in Spring
Factory Pattern Focus
| Category | Purpose | Examples |
|---|---|---|
| Creational | Object creation | Factory, Singleton, Builder |
| Structural | Object composition | Adapter, Decorator, Proxy |
| Behavioral | Object communication | Strategy, Observer, Template |
Why It Matters
- Centralizes object creation
- Hides implementation details from callers
- Matches how Spring containers supply managed beans
Lecture 31: Strategy Design Pattern in Spring
Strategy Pattern Focus
- Encapsulates interchangeable algorithms behind one interface
- Works naturally with Spring DI using multiple bean implementations
- Often combined with
@Qualifierto choose behavior
public interface SortStrategy { void sort(int[] data); }
class QuickSort implements SortStrategy { /* ... */ }
class MergeSort implements SortStrategy { /* ... */ }
public class Sorter {
private SortStrategy strategy;
public Sorter(SortStrategy s) { this.strategy = s; }
public void sort(int[] data) { strategy.sort(data); }
}Lecture 32: AOP & Bean Configuration Styles (XML and Annotation)
AOP Terminology
| Term | Meaning |
|---|---|
| Aspect | Module holding cross-cutting logic |
| Advice | The code that runs (Before/After/Around) |
| Pointcut | Rule selecting which methods to target |
| Join Point | Specific method execution point |
| Weaving | Applying aspect to target classes |
Logging Aspect Example
@Aspect @Component
public class LoggingAspect {
@Pointcut("execution(* com.example.service.*.*(..))")
public void serviceLayer() {}
@Before("serviceLayer()")
public void logBefore(JoinPoint jp) {
System.out.println("Entering: " + jp.getSignature().getName());
}
@Around("serviceLayer()")
public Object measureTime(ProceedingJoinPoint pjp) throws Throwable {
long start = System.currentTimeMillis();
Object res = pjp.proceed();
System.out.println(pjp.getSignature() + " took " +
(System.currentTimeMillis() - start) + "ms");
return res;
}
}Autowiring and Qualifiers
@Autowired— inject matching bean by type@Primary— default bean when multiple candidates exist@Qualifier("beanName")— select specific bean by name
Spring Stereotype Annotations
@Component— generic managed bean@Service— business logic layer@Repository— data access layer (with exception translation)@Controller/@RestController— web MVC layer
Configuration Style Summary
| Style | Key Annotation | Best For |
|---|---|---|
| XML | <bean> |
Legacy projects |
| Java Config | @Configuration + @Bean |
Infrastructure beans |
| Annotation | @Component + @Autowired |
Application classes |
Key Takeaways
- Design patterns explain how Spring organizes flexibility and object creation
- Factory and Strategy appear naturally in container-managed applications
- AOP handles cross-cutting concerns without mixing them into core logic
- XML, Java config, and annotations are three major configuration approaches
Next Block
Unit V begins with Spring Boot configuration, REST controllers, and API design.