Comprehensive Overview of the Spring Framework: Core Concepts, Modules, IoC, AOP, and MVC
This article provides a detailed English-language overview of the Spring Framework, covering its lightweight nature, IoC container, AOP support, core modules, bean lifecycle, configuration methods, annotations, transaction management, and Spring MVC architecture in a concise yet thorough manner.
1. Role of the Spring Framework
Lightweight: the core distribution is about 2 MB.
Inversion of Control (IoC): Spring achieves loose coupling by letting objects declare their dependencies instead of creating or looking them up.
Aspect‑Oriented Programming (AOP): Spring supports AOP to separate business logic from system services.
Container: Spring manages the lifecycle and configuration of application objects.
MVC framework: Spring‑MVC.
Transaction management: a unified API that can work from local transactions up to global JTA transactions.
Exception handling: convenient APIs that translate technology‑specific exceptions.
2. Composition of Spring
Spring consists of seven modules:
Spring Core: provides the basic functionality; the main component is BeanFactory, an implementation of the Factory pattern that separates configuration and dependencies from code.
Spring Context: supplies configuration information and enterprise services such as JNDI, EJB, mail, i18n, validation and scheduling.
Spring AOP: integrates AOP features directly into the framework, enabling declarative transaction management without EJB.
Spring DAO: offers a JDBC abstraction layer with a consistent exception hierarchy that simplifies error handling.
Spring ORM: integrates ORM tools (JDO, Hibernate, iBatis) and applies Spring’s transaction and DAO exception handling.
Spring Web: builds on the application‑context module to support web‑based applications, multipart requests and parameter binding.
Spring MVC: a full‑featured MVC implementation supporting many view technologies (JSP, Velocity, Tiles, iText, POI).
3. Spring Containers
Two main container types:
BeanFactory: the simplest container providing basic DI support; the most common implementation is XmlBeanFactory, which reads bean definitions from XML.
ApplicationContext: built on top of BeanFactory and offers additional application‑level services.
4. Typical ApplicationContext Implementations
ClassPathXmlApplicationContext – loads XML configuration from the classpath.
FileSystemXmlApplicationContext – loads XML configuration from the file system.
XmlWebApplicationContext – loads XML configuration from a web application.
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");5. IoC & DI
Inversion of Control is realized mainly through Dependency Injection (DI) and, less commonly, Dependency Lookup. Spring’s IoC container creates, assembles, configures and manages the full lifecycle of objects.
Advantages include reduced code, easy testing, and support for eager or lazy loading. DI can be performed via constructor injection or setter injection.
6. Supplying Configuration Metadata to the Spring Container
XML configuration files.
Annotation‑based configuration.
Java‑based configuration using @Configuration and @Bean.
7. Attributes of the <bean> Tag
id, name, class, init‑method, destroy‑method, factory‑method, scope (singleton, prototype, request, session, global‑session), autowire (byName, byType, constructor, autodetect).
8. Attributes of the <beans> Tag
default‑init‑method, default‑destroy‑method, default‑autowire (default is none).
9. Bean Lifecycle
Instantiate the bean (factory‑method, constructor autowire).
Populate properties (autowireByName, autowireByType).
Initialize the bean:
Invoke Aware methods (BeanNameAware, BeanFactoryAware, ApplicationContextAware, etc.).
Apply BeanPostProcessor before initialization.
Call custom init‑method or afterPropertiesSet() if the bean implements InitializingBean.
Apply BeanPostProcessor after initialization.
Bean is ready for use.
When the context is closed, invoke destroy‑method or DisposableBean.destroy().
10. Injecting Collections
<list> – allows duplicate values.
<set> – disallows duplicates.
<map> – arbitrary key/value types; keys can be refs.
<props> – only String keys and values.
11. Injecting Null Values
(Illustrated with an image.)
12. Autowiring
Reduces or eliminates the need for <property> and <constructor‑arg> elements; Spring automatically resolves bean dependencies. <context:annotation-config/> enables annotation‑driven injection, while <context:component-scan/> enables autodetection of candidate components.
13. Annotations
Spring disables annotation injection by default; it can be enabled with <context:annotation-config/>. Supported injection annotations include @Autowired (Spring), @Inject (JSR‑330) and @Resource (JSR‑250).
14. @Autowired
@Autowired requires a matching bean; otherwise a NoSuchBeanDefinitionException is thrown. The required attribute can be set to false to make the injection optional.
15. Custom Qualifiers
Custom annotations such as @SpecialQualifier can narrow the candidate beans for @Autowired, optionally combined with additional qualifiers.
16. Pros and Cons of @Autowired
Reduces XML configuration but introduces a direct dependency on Spring’s annotation infrastructure.
17. @Inject
Functions like @Autowired but lacks a required attribute; the dependency must be present.
18. @Named
Corresponds to the qualifier used with @Inject.
19. Spring Expression Language (SpEL)
Expressions are written inside #{...} and can be used to compute values at runtime.
20. @Value
Injects literal values or results of SpEL expressions into fields, method parameters or constructor arguments.
21. Automatic Bean Detection
<context:component-scan>scans the specified base packages and registers beans annotated with stereotype annotations.
22. Stereotype Annotations
@Component – generic component.
@Controller – Spring MVC controller.
@Repository – data‑access component.
@Service – service layer component.
23. AOP Overview
Aspect‑Oriented Programming modularizes cross‑cutting concerns such as logging and transaction management. A Spring aspect is a class annotated with @Aspect that defines pointcuts and advice.
24. Types of Advice
before – executed before the method.
after – executed after the method regardless of outcome.
after‑returning – executed only on successful completion.
after‑throwing – executed when an exception is thrown.
around – wraps the method execution.
25. Transaction Types
Programmatic transaction management offers flexibility but is harder to maintain; declarative transaction management separates business code from transaction handling using annotations or XML.
26. ACID Properties
Atomicity – all operations succeed or none do.
Consistency – the system remains in a valid state after the transaction.
Isolation – concurrent transactions do not interfere.
Durability – committed changes survive system failures.
27. JDBC Transactions
When using JDBC directly (e.g., with MyBatis), Spring’s DataSourceTransactionManager can demarcate transaction boundaries.
28. JTA Transactions
JtaTransactionManager coordinates transactions across multiple resources (multiple databases, JMS, etc.) by delegating to a JTA implementation.
29. Declarative Transaction Attributes
Isolation level (DEFAULT, READ_COMMITTED, READ_UNCOMMITTED, REPEATABLE_READ, SERIALIZABLE).
Propagation behavior (REQUIRED, SUPPORTS, MANDATORY, REQUIRES_NEW, NOT_SUPPORTED, NEVER, NESTED).
Read‑only flag.
Timeout.
Rollback rules (rollback‑for, no‑rollback‑for).
30. Spring MVC Architecture
The request flow is: DispatcherServlet → HandlerMapping → HandlerAdapter → Controller → ModelAndView → ViewResolver → View → render → response.
31. DispatcherServlet
The front controller for Spring MVC; configured in web.xml and loads an XML context named after the servlet.
32. Configuring HandlerMapping
Spring provides several implementations such as BeanNameUrlHandlerMapping, ControllerClassNameHandlerMapping, DefaultAnnotationHandlerMapping, SimpleUrlHandlerMapping, etc.
33. Configuring HandlerAdapter
HandlerAdapters adapt various controller types to the DispatcherServlet.
34. Configuring Views
InternalResourceViewResolver follows a convention‑over‑configuration approach, resolving logical view names to JSP files under /WEB-INF/jsp/.
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.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
