Comprehensive Overview of the Spring Framework: Core Concepts, Modules, IoC, AOP, Bean Lifecycle, and MVC
This article provides an in‑depth, English‑language overview of the Spring framework, covering its purpose, core modules, IoC and AOP principles, bean scopes and lifecycle, transaction management, MVC architecture, and the design patterns it employs, all without heavy code examples.
What is the Spring Framework?
Spring is a lightweight development framework that aims to improve developer productivity and system maintainability. It consists of many modules, including Core Container, Data Access/Integration, Web, AOP, Tools, Messaging, and Testing.
Key Features of Spring
Core Technology : Dependency Injection (DI), AOP, events, resources, i18n, validation, data binding, type conversion, SpEL.
Testing : mock objects, TestContext framework, Spring MVC testing, WebTestClient.
Data Access : transaction management, DAO support, JDBC, ORM, XML mapping.
Web Support : Spring MVC and Spring WebFlux.
Integration : remote handling, JMS, JCA, JMX, email, scheduling, caching.
Language : Kotlin, Groovy, dynamic languages.
Important Spring Modules
Spring Core : fundamental container providing IoC and dependency injection.
Spring Aspects : integration support for AspectJ.
Spring AOP : implements aspect‑oriented programming.
Spring JDBC : Java Database Connectivity utilities.
Spring JMS : Java Message Service support.
Spring ORM : integration with Hibernate and other ORM tools.
Spring Web : support for building web applications.
Spring Test : provides JUnit and TestNG testing support.
Understanding Spring IoC
IoC (Inversion of Control) is a design principle that transfers the responsibility of object creation from the program to the Spring container, which acts like a map of key‑value pairs holding bean instances. This simplifies development, improves maintainability, and reduces coupling.
IoC Initialization Process
IoC Source Code Snippet
set()– sets property values via reflection. BeanNameAware – receives the bean name via setBeanName(). BeanClassLoaderAware – receives the class loader via setBeanClassLoader(). BeanFactoryAware – receives the bean factory via setBeanFactory(). *.Aware – other aware interfaces are handled similarly. BeanPostProcessor – postProcessBeforeInitialization() and postProcessAfterInitialization() are invoked. InitializingBean – afterPropertiesSet() is called.
Custom init-method and destroy-method are executed if defined. DisposableBean – destroy() is called on bean destruction.
Understanding Spring AOP
AOP (Aspect‑Oriented Programming) extracts cross‑cutting concerns such as transaction handling, logging, and security into reusable aspects, reducing duplicate code and coupling. Spring AOP is based on dynamic proxies: JDK proxies are used when the target implements an interface; otherwise CGLIB creates a subclass proxy.
Spring AOP vs. AspectJ AOP
Spring AOP performs runtime proxy‑based enhancement, while AspectJ performs compile‑time bytecode manipulation. AspectJ is more powerful but Spring AOP is simpler and sufficient for most use cases.
Bean Scopes in Spring
singleton – one shared instance (default).
prototype – a new instance on each request.
request – a new instance per HTTP request.
session – a new instance per HTTP session.
global‑session – scoped to a Portlet‑based application (removed in Spring 5).
Thread‑Safety of Singleton Beans
Singleton beans can suffer from thread‑safety issues when mutable fields are accessed by multiple threads. Common solutions are to avoid mutable fields or to store mutable state in a ThreadLocal variable.
Spring Bean Lifecycle
The lifecycle includes discovery of bean definitions, instance creation via reflection, property population, aware‑interface callbacks, BeanPostProcessor invocations, initialization callbacks ( InitializingBean, custom init‑method), and destruction callbacks ( DisposableBean, custom destroy‑method).
Spring MVC Overview
Before Spring MVC, Java web development used Model1 (JSP only) and Model2 (Servlet + JSP). Spring MVC separates concerns into Controller, Service, DAO, Entity, and View layers, offering higher productivity and performance.
Spring MVC Request Processing Flow
Client sends request to DispatcherServlet. DispatcherServlet uses HandlerMapping to locate the appropriate Handler (controller). HandlerAdapter invokes the controller method.
Controller returns a ModelAndView containing model data and logical view name. ViewResolver resolves the logical view to a concrete view implementation. DispatcherServlet renders the view with the model data and sends the response back to the client.
Design Patterns Used in Spring
Factory Pattern – BeanFactory and ApplicationContext create beans.
Proxy Pattern – implements AOP.
Singleton Pattern – default bean scope.
Template Method Pattern – JdbcTemplate, HibernateTemplate, etc.
Decorator Pattern – dynamic data‑source routing.
Observer Pattern – Spring’s event model.
Adapter Pattern – AOP advice adapters and MVC controller adapters.
@Component vs. @Bean
@Componentannotates a class; @Bean annotates a method. @Component is discovered via class‑path scanning; @Bean is declared in a @Configuration class. @Bean offers finer‑grained control and is required for third‑party classes that cannot be annotated.
Example of @Bean
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}Equivalent XML
<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>Annotations to Declare a Spring Bean
@Component– generic stereotype. @Repository – DAO layer. @Service – service layer. @Controller – MVC controller layer.
Transaction Management in Spring
Programmatic transactions (hard‑coded, not recommended).
Declarative transactions (preferred) via XML or annotations.
Transaction Isolation Levels
ISOLATION_DEFAULT – uses the database’s default.
ISOLATION_READ_UNCOMMITTED – allows dirty reads.
ISOLATION_READ_COMMITTED – prevents dirty reads.
ISOLATION_REPEATABLE_READ – prevents dirty and non‑repeatable reads.
ISOLATION_SERIALIZABLE – highest isolation, prevents all anomalies.
Transaction Propagation Behaviors
PROPAGATION_REQUIRED – join existing or create new.
PROPAGATION_SUPPORTS – join if exists, otherwise non‑transactional.
PROPAGATION_MANDATORY – must have an existing transaction.
PROPAGATION_REQUIRES_NEW – suspend current and start a new one.
PROPAGATION_NOT_SUPPORTED – execute non‑transactionally, suspending any existing transaction.
PROPAGATION_NEVER – must not run within a transaction.
PROPAGATION_NESTED – run within a nested transaction if one exists.
References
"Spring in Action" and other Spring books.
http://www.cnblogs.com/wmyskxz/p/8820371.html
https://www.journaldev.com/2696/spring-interview-questions-and-answers
https://www.edureka.co/blog/interview-questions/spring-interview-questions/
https://howtodoinjava.com/interview-questions/top-spring-interview-questions-with-answers/
http://www.tomaszezula.com/2014/02/09/spring-series-part-5-component-vs-bean/
https://stackoverflow.com/questions/34172888/difference-between-bean-and-autowired
(End)
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.
