Backend Development 9 min read

Introduction to Spring Framework and Dependency Injection

This article introduces the Spring Framework, explains its layered architecture and core modules, and details fundamental concepts such as Dependency Inversion Principle, Dependency Injection, BeanFactory, BeanDefinition, and ApplicationContext, along with practical Java‑based configuration and annotation‑driven wiring examples.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Introduction to Spring Framework and Dependency Injection

1. Spring Overview

1. Spring simplifies Java development

Spring Framework is an application framework that provides a semi‑finished foundation, allowing developers to focus on business logic instead of building architecture, infrastructure, and common components from scratch. Its goal is to simplify Java development through decoupling, design principles, and annotation‑based configuration.

(1) Decoupling in architecture: Dependency Injection (DI) manages type dependencies, and Aspect‑Oriented Programming (AOP) separates concerns, reducing duplicate code.

(2) Design principles: widespread use of DIP, ISP, Facade, etc., offering simplified interfaces and encapsulating many third‑party components.

(3) Language level: annotations simplify configuration, similar to .NET attributes.

2. Spring Framework architecture and modules

Spring follows a loosely coupled layered architecture where outer layers may depend on inner layers, but not vice‑versa. The core components include core , beans , aop , and context , with later extensions such as context-support , expression , and beans-groovy .

The core modules spring-oxm , spring-jdbc , and spring-web are heavily depended upon; spring-oxm provides Object‑XML mapping support.

2. Basic Concepts

1. DIP: The Dependency Inversion Principle is the core of DI. High‑level modules should not depend on low‑level modules; both should depend on abstractions, and abstractions should not depend on details.

2. DI: Dependency Injection eliminates the need to write factory code for mapping interfaces to implementations, object creation, and lifecycle management.

• Interface injection – rarely used today. • Constructor injection – dependencies appear as constructor parameters. • Property injection – dependencies appear as bean properties.

DI tools provide configuration (code or file), an object factory that returns one or more instances, and lifecycle management (singleton, prototype, request, etc.).

3. Key Points of Spring Dependency Injection

In Spring, a Bean is simply a POJO.

The core types to master are BeanDefinition , BeanFactory , and ApplicationContext .

1. BeanFactory

BeanFactory is the central interface for DI, designed with ISP. The most important implementation is DefaultListableBeanFactory , which stores bean definitions in a Map<String, BeanDefinition> .

XmlBeanFactory loads bean definitions via XmlBeanDefinitionReader , which parses XML into BeanDefinition objects.

2. BeanDefinition

BeanDefinition holds configuration metadata, regardless of whether the configuration is expressed in Java code, XML, annotations, or Groovy scripts.

3. ApplicationContext

ApplicationContext delegates object‑factory responsibilities to a BeanFactory implementation, typically DefaultListableBeanFactory . Common variants are AnnotationConfigApplicationContext and AnnotationConfigWebApplicationContext .

4. Quick Start with Spring DI

1. Use Java configuration instead of XML

Java configuration relies on @Configuration and @Bean . Bean scope is defined with @Scope , and additional configuration files can be imported with @Import .

• @Configuration marks a class as a configuration source (equivalent to an XML file). • @Bean marks a method as a bean definition (equivalent to an XML element).

For web applications, use AnnotationConfigWebApplicationContext ; the context can be obtained in JSP via WebApplicationContextUtils .

2. Annotation‑based auto‑wiring

Auto‑wiring primarily uses @ComponentScan , @Component , and @Autowired .

• @ComponentScan on a configuration class enables scanning for types annotated with @Component , @Controller , @Service , or @Repository . • @Autowired injects dependencies by type (similar to @Inject ); @Resource injects by name.

Summary

You should at least know:

BeanFactory and BeanDefinition

DefaultListableBeanFactory and ApplicationContext

AnnotationConfigApplicationContext and AnnotationConfigWebApplicationContext

@Configuration and @Bean

Spring’s DI is object‑centric rather than type‑centric; it originally supported XML configuration before annotations, and although annotation‑based configuration is now dominant, the underlying object‑based model still influences many aspects.

Javabackend developmentSpringdependency injectionDISpring Framework
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.