Spring Framework Core
The foundation of modern Java development. Master IoC, Dependency Injection, and AOP. Learn to build loosely coupled, testable, and maintainable enterprise applications.
1. Phase Overview
The Spring Framework is the backbone of Java enterprise development. This phase introduces the core concepts that changed the Java landscape: Inversion of Control (IoC) and Dependency Injection (DI). You'll master how Spring manages your application's lifecycle and dependencies, moving away from manual object creation to a managed, declarative approach.
2. Why This Phase Matters
In the traditional Java EE era, applications were tightly coupled and difficult to test. Spring Core solves this by:
- Decoupling Logic: Business logic is separated from infrastructure and object creation.
- Testability: Objects can be easily mocked since they receive dependencies from the outside.
- Standardization: Provides a unified way to manage application configuration.
- AOP: Declaratively handles cross-cutting concerns like logging and transactions.
3. Complete Theoretical Roadmap
| Concept | Internal Working | Enterprise Benefit |
|---|---|---|
| IoC Container | ApplicationContext & BeanFactory | Centralized object management and configuration. |
| Dependency Injection | Constructor, Setter, and Field Injection | Eliminates hard-coded dependencies for better flexibility. |
| Bean Lifecycle | Initialization, Post-Processors, Destruction | Allows custom logic at various stages of an object's life. |
| AOP | JDK Dynamic Proxies & CGLIB | Clean separation of business logic from infrastructure. |
4. Deep Topic Breakdown
4.1 IoC vs DI Theory
Inversion of Control (IoC): A design principle where the control of object creation and lifecycle is shifted from the programmer to a container or framework.
Dependency Injection (DI): A design pattern that implements IoC. It's the process of supplying a resource (dependency) to a consuming object.
4.2 The Bean Lifecycle
Spring manages beans through a complex, yet predictable lifecycle:
- Instantiation: The container creates the bean.
- Populate Properties: DI occurs here.
- BeanNameAware / BeanFactoryAware: Setting the bean's context.
- Pre-Initialization (BeanPostProcessor): Custom logic before init.
- @PostConstruct / init-method: The bean's custom initialization.
- Post-Initialization (BeanPostProcessor): Custom logic after init.
- Ready for Use: The bean is active in the container.
- @PreDestroy / destroy-method: Cleanup before the container shuts down.
4.3 Proxies & AOP Mechanics
Spring AOP uses two types of proxies to implement cross-cutting concerns:
- JDK Dynamic Proxies: Used when the target class implements at least one interface.
- CGLIB: Used when the target class does not implement any interfaces (it subclasses the target).
5. Subtopic Curriculum
6. Chronological Progression
Week 1: The Container Engine
Introduction to the ApplicationContext, manual bean configuration in Java, and the DI flow.
Week 2: Stereotypes & Autowiring
Mastering @Component scanning, primary vs qualifier, and circular dependency resolution.
Week 3: Advanced Scopes & Lifecycle
Deep dive into BeanPostProcessors, custom scopes, and managing resources via Lifecycle callbacks.
Week 4: AOP & Proxies
Writing aspects, understanding JoinPoints and Pointcuts, and the internal proxy mechanism.
7. Weekly Mastery Roadmap
- Week 1: Can create a Spring application without any XML configuration.
- Week 2: Understands when to use Constructor vs Setter injection.
- Week 3: Successfully implements a custom BeanPostProcessor.
- Week 4: Can explain why AOP methods don't work when called internally (Proxy self-invocation issue).
8. Practice Tasks
- Build a Notification Service that injects different implementations (Email, SMS) based on profiles.
- Create a Global Configuration Logger using AOP to log every method entry in the service layer.
- Implement a Prototype-scoped Bean and observe its behavior when injected into a Singleton.
9. Exercises
Theoretical Challenges
- Why does Spring prefer Constructor Injection over Field Injection?
- What is the difference between @Primary and @Qualifier?
- Explain how Spring handles Circular Dependencies in Singleton beans.
10. Theory Checkpoints
- Do you know the difference between ApplicationContext and BeanFactory?
- Can you explain the role of a BeanDefinition?
- Do you understand how @Transactional works under the hood (Hint: It's AOP)?
11. Mini Projects
1. Custom Event Listener
Implement a system where components communicate via Spring's ApplicationEventPublisher.
2. Profile-based DB Configurator
Switches between H2 and MySQL configurations using @Profile.
12. Major Phase Project
Spring-Powered Inventory System
Refactor your Library Management System to use the Spring IoC container.
- Features: Annotation-based config, @Service and @Repository layers, and AOP-based logging.
- Tech: Spring Core, Spring Context, AspectJ.
13. Enterprise Bookstore Case Study
Phase Implementation: Moving from manual instantiation to Spring Managed Beans.
BookService: Marked as@Serviceand injected with dependencies.InventoryRepository: Marked as@Repository.LoggingAspect: Automatically logs all bookstore transactions.
Focus: Decoupling the business logic from the specific implementation of the repository.
14. Architecture Mapping
Spring Core is the Glue Layer of your enterprise application.
- Service Layer: Becomes managed beans for easy transaction control.
- Cross-cutting Layer: Aspects handle security and auditing without polluting business code.
- Configuration Layer: Centralized Java config classes replace scattered property files.
15. Interview Preparation
A: Singleton. Only one instance of the bean is created per container.
A: Technically they are identical (stereotypes), but they convey semantic meaning and can be used for specific post-processing (e.g., @Repository enables Exception Translation).
A: For Singleton beans using setter injection, Spring uses a three-stage cache (early singleton objects) to provide a partially initialized bean to resolve the cycle.
16. Common Mistakes
- Field Injection: Makes unit testing difficult and hides dependencies.
- Misusing Scopes: Injecting a Prototype bean into a Singleton bean without using a Lookup method or Scoped Proxy.
- Over-AOP: Making the code hard to follow by putting too much logic in aspects.
17. Best Practices
- Use Constructor Injection: Ensures all required dependencies are present and allows for immutable beans.
- Program to Interfaces: Inject interfaces, not implementations, to maintain decoupling.
- Externalize Configuration: Use @Value or @ConfigurationProperties for environment-specific values.
18. Recommended Tools
- Spring Initializr: start.spring.io for project bootstrapping.
- IDE Support: Spring Tools 4 or IntelliJ Ultimate's Spring Support.
- VisualVM: To observe bean counts and memory usage.
19. Relevant Certifications
- Spring Professional Develop Certificate (VMware/Broadcom).
20. Free Resources
21. Essential Documentation
22. GitHub Portfolio Roadmap
- Repository:
spring-core-masterywith examples of all DI types and AOP aspects. - Updated
bookstore-backendnow running on Spring IoC.
23. How to Showcase on Resume
Bullet Point: "Architected a decoupled inventory management system using Spring IoC and AOP, reducing component coupling by 40% and implementing declarative logging via custom aspects."
24. Career Outcomes
- Mid-level Java Developer.
- Spring Backend Engineer.