Mastering Spring: Core Concepts, IoC, DI, AOP, and Essential Annotations
An in‑depth guide to the Spring framework covering its lightweight architecture, core principles such as IoC, DI, and AOP, key design patterns, module structure, bean lifecycle, circular dependency handling, and essential annotations for building robust Java backend applications.
Java developers often encounter the Spring framework, which has become a standard for enterprise‑level application development. From early SSH/SSM to modern Spring MVC, Spring Boot, and Spring Cloud, Spring continuously simplifies Java development.
Overview
Spring is a lightweight Java development framework created by Rod Johnson to solve coupling problems between business‑logic and other layers. It provides a full‑stack, non‑intrusive container that manages the infrastructure so developers can focus on application code.
Its fundamental mission is to reduce the complexity of enterprise applications by simplifying Java development.
Spring’s two core features are Dependency Injection (DI) and Aspect‑Oriented Programming (AOP) .
Key Strategies
Lightweight, POJO‑based programming with minimal intrusion.
Loose coupling through DI and interface‑based design.
Declarative programming via aspects and conventions.
Reduction of boiler‑plate code using templates and aspects.
Core Concepts
IOC (Inversion of Control) – The container creates and manages objects using a factory (BeanFactory), so developers obtain beans from the container instead of instantiating them directly.
Benefits: simplifies object management, reduces code, and improves development efficiency.
DI (Dependency Injection) – Spring injects required dependencies via setter, constructor, or interface injection, allowing services to obtain collaborators without manual creation.
AOP (Aspect‑Oriented Programming) – Separates cross‑cutting concerns (e.g., security, transactions, logging) into reusable aspects applied horizontally across the application.
Design Goals and Philosophy
Provide a one‑stop lightweight platform for Java EE development.
Support POJO/JavaBean development, interface‑driven design, and full OO principles.
Manage object relationships through an IoC container, achieving loose coupling.
Advantages and Disadvantages
Decouples object creation, supports AOP, declarative transactions, testing with JUnit, integrates many frameworks, simplifies difficult Java EE APIs.
Perceived as large despite being lightweight, relies on reflection (performance impact), steep learning curve.
Modules
Spring consists of roughly 20 modules grouped into six core areas: Core Container, AOP, Instrumentation, Data Access/Integration, Web, Messaging, and Test. Key modules include:
spring‑core : IOC and DI core.
spring‑beans : BeanFactory implementation.
spring‑context : ApplicationContext utilities.
spring‑jdbc : Simplified JDBC abstraction.
spring‑aop : AOP support.
spring‑web : Web MVC features.
spring‑test : Testing support for JUnit/TestNG.
Design Patterns Used
Factory (BeanFactory).
Singleton (default bean scope).
Proxy (JDK dynamic proxy, CGLIB).
Template Method (e.g., RestTemplate, JmsTemplate).
Observer (ApplicationListener).
Bean Lifecycle
The entry point for bean creation is ConfigurableListableBeanFactory#createBean. The lifecycle diagram is illustrated below:
Circular Dependency Resolution
Spring uses three cache levels to resolve circular references: singletonFactories, earlySingletonObjects, and singletonObjects. The container checks for circular dependencies before populateBean and may expose early references.
// If RootBeanDefinition is a singleton and circular references are allowed
boolean earlySingletonExposure = mbd.isSingleton()
&& this.allowCircularReferences
&& this.isSingletonCurrentlyInCreation(beanName);
if (earlySingletonExposure) {
this.addSingletonFactory(beanName, () -> this.getEarlyBeanReference(beanName, mbd, bean));
}IOC Implementation Details
Spring’s IoC container builds beans via factory methods, managing their lifecycle and dependencies. Detailed explanations are available in external blogs (e.g., Iteye).
Annotations Overview
Spring supports numerous annotations for component scanning and configuration, such as @Component, @Controller, @Service, @Repository, @Autowired, @RequestMapping, @Cacheable, @Resource, @PostConstruct, @PreDestroy, @Scope, @SessionAttributes, @Qualifier, etc.
Examples:
@Configuration
public class StudentConfig {
@Bean
public StudentBean myStudent() {
return new StudentBean();
}
}Using @Controller and @RequestMapping:
@Controller
public class TestController {
@RequestMapping("/test")
public String test(Map<String,Object> map) {
return "hello";
}
}Other annotations like @Cacheable, @CacheEvict, @ModelAttribute, and @Scope control caching, model binding, and bean scopes (singleton, prototype, request, session, global session).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Intelligent Backend & Architecture
We share personal insights on intelligent, automated backend technologies, along with practical AI knowledge, algorithms, and architecture design, grounded in real business scenarios.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
