Introduction to the Spring Framework: Core Concepts, Bean Management, and AOP
This article provides a comprehensive overview of the Spring framework, covering its low‑invasion design, IoC container, dependency injection types, bean scopes, autowiring options, bean creation methods, post‑processors, zero‑configuration annotations, and AOP support with AspectJ, supplemented by practical code examples.
Spring, created by Rod Johnson and first released in 2004, is a lightweight Java framework that abstracts common development steps, allowing developers to focus on application‑specific logic and thereby improving enterprise development efficiency.
Key advantages of Spring include low‑invasion design, server‑independence (Write Once, Run Anywhere), IoC container‑based decoupling, AOP‑driven cross‑cutting concerns, seamless ORM/DAO integration, and optional modular usage.
Core mechanism – Bean management : The Spring container (typically accessed via ApplicationContext) creates and manages beans. Two common implementations are ClassPathXmlApplicationContext (loads XML from the classpath) and FileSystemXmlApplicationContext (loads XML from a file system path).
Example of retrieving a bean:
public class BeanTest {
public static void main(String args[]) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Person p = ctx.getBean("person", Person.class);
p.say();
}
}In IDEs such as Eclipse, Spring JARs can be added via a User Library or placed directly in /WEB-INF/lib. When using a User Library, the JARs must be copied to /WEB-INF/lib during deployment.
Dependency Injection (DI) is the core of Spring. The container creates beans and injects their dependencies either through setter‑based (property) injection or constructor injection. Setter injection is intuitive and suitable for complex graphs, while constructor injection guarantees immutable dependencies and clear injection order.
Bean scopes supported by Spring are:
singleton – one instance per container (default)
prototype – a new instance each request to getBean() request – one instance per HTTP request (Web only)
session – one instance per HTTP session (Web only)
global session – one instance per global portlet session
Autowiring can be enabled via the default-autowire attribute on <beans> or the autowire attribute on individual <bean> definitions. Supported modes are no, byName, byType, constructor, and autodetect. Explicit ref definitions override autowiring.
Spring provides three ways to create a bean:
Using a constructor (default, requires a no‑arg constructor if no constructor injection is defined)
Using a static factory method (specify class as the factory class and factory-method)
Using an instance factory method (specify factory-bean and factory-method)
When a singleton bean depends on a prototype bean, synchronization issues may arise. Two solutions are: (1) manually request a new prototype bean each time, or (2) use method injection (lookup‑method) so the container overrides an abstract method to return a fresh prototype.
Spring also defines two post‑processor types:
BeanPostProcessor : implements postProcessBeforeInitialization and postProcessAfterInitialization to enhance individual beans after creation.
BeanFactoryPostProcessor : implements postProcessBeanFactory to modify the container itself before any beans are instantiated.
Zero‑configuration support is achieved with component‑scanning annotations such as @Component, @Controller, @Service, and @Repository, enabled by <context:component-scan base-package="..."/>. Dependency injection can also be expressed with @Resource, while bean lifecycle callbacks use @PostConstruct and @PreDestroy. Precise autowiring is possible with @Autowired together with @Qualifier.
Aspect‑Oriented Programming (AOP) in Spring addresses cross‑cutting concerns such as transactions, security, and logging. Spring supports both static (AspectJ compile‑time weaving) and dynamic (Spring AOP runtime proxies) implementations. To enable @AspectJ support, include aspectjweaver.jar, aspectjrt.jar, and aopalliance.jar, and configure:
<aop:aspectj-autoproxy/>
<context:component-scan base-package="...">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>The article concludes with a note that further interview questions will be covered in the next part.
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.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.
