A Comprehensive Overview of Spring Framework Concepts and Practices
This article provides a systematic summary of Spring's core concepts—including IoC, AOP, bean configuration via XML and annotations, dependency injection mechanisms, transaction management, and web integration—offering Java developers a clear guide to effectively using the framework in real-world projects.
If there is any framework that Java programmers inevitably learn and use, Spring is certainly one of them. This article systematically summarizes the author's daily work experience with Spring.
Spring Concepts and Ideas
Spring's core: AOP and IoC.
In simple terms, objects are handed over to Spring for management, and aspect‑oriented programming provides template‑style operations, freeing developers to focus on business logic.
Spring is a one‑stop open‑source solution with a rich ecosystem.
About IoC
IoC can be configured via XML or annotations, with annotations becoming increasingly popular in practice.
The main configuration file is usually named applicationContext-xxx.xml , which allows modular management and easy regex loading of multiple files.
Spring
Bean XML Configuration
Beans are defined with <bean id="" class="">...</bean> . The class must have a no‑argument constructor because Spring instantiates it via reflection.
Bean scope can be specified with the scope attribute, e.g., singleton (default) or prototype , as well as request/session scopes.
singleton : single instance, default
prototype : multiple instances
request / session, etc.
Bean Property Injection (XML)
Properties can be injected via constructor arguments, setter methods, or by referencing other beans.
<bean id="" class="">
<property name="" value="">
<property name="" ref="">
</bean>Annotation‑Based Bean Creation and Injection
Enable annotation scanning in XML with <context:component-scan base-package="" /> , which discovers classes annotated with @Component, @Controller, @Service, or @Repository.
These annotations can specify a bean ID via the value attribute and define scope with @Scope .
Dependency Injection Annotations
Use @Autowired , @Resource , and @Qualifier to inject dependencies.
@Resource (from J2EE) defaults to name‑based injection, falling back to type if no matching name is found.
@Autowired defaults to type‑based injection and can be combined with @Qualifier for name‑based selection.
IoC vs DI
IoC (Inversion of Control) is the principle of handing objects to the container; DI (Dependency Injection) is the concrete mechanism that the container uses to set object properties.
About AOP
AOP provides cross‑cutting concerns using dynamic proxies.
Key AOP concepts:
JoinPoint: a method that can be enhanced.
PointCut: the set of JoinPoints to intercept.
Advice: the action taken after interception.
Aspect: the combination of pointcut and advice.
Spring AOP relies on AspectJ and can be configured via XML or annotations. For example, enable AOP with <aop:aspectj-autoproxy /> and use @Before("execution(...)") on methods.
Spring Transaction Management
Declarative transaction management is most often used with annotations. A transaction manager must be configured with a DataSource, and different DAO frameworks (JDBC, MyBatis, Hibernate, etc.) have their own transaction implementations.
Spring Annotation Transaction Configuration
For multiple data sources, define multiple transaction managers and enable annotation‑driven transactions, distinguishing them with the qualifier attribute.
Apply transactions in service layer methods with @Transactional(value = "gcs", rollbackFor = Exception.class) .
Spring and Web Integration
Configure a listener in web.xml that loads Spring configuration files and stores created objects in the ServletContext for later retrieval.
That concludes the systematic overview of Spring.
PS: If you find this sharing useful, please like and forward.
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.