Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Master Spring’s Core: IoC, DI, AOP & Transaction Explained

Master Spring’s Core: IoC, DI, AOP & Transaction Explained

This article provides a comprehensive overview of the Spring framework, covering its core principles such as Inversion of Control, Dependency Injection, Aspect‑Oriented Programming, and transaction management, while illustrating each concept with diagrams and practical code examples for Java developers.

Spring Framework Overview

The Spring framework consists of modules such as Spring IoC and Spring AOP, as illustrated in the diagram below.

Spring modules diagram
Spring modules diagram

Spring Core Principles

The core principles of Spring are Inversion of Control (IoC) and Dependency Injection (DI), together with AOP and transaction mechanisms.

Spring IoC Principle

IoC (Inversion of Control) is the heart of Spring. It delegates object creation, assembly, and lifecycle management to the container, reducing tight coupling and improving testability.

Traditional applications instantiate dependencies manually, leading to high coupling. With an IoC container, the control of creating and locating dependent objects is handed over to the container, as shown in the diagram.

IoC container diagram
IoC container diagram

The container creates, instantiates, assembles, and provides beans according to the following steps:

Define a bean using annotations such as @Component or @Service.

Instantiate the bean when it is first needed.

Inject dependencies into the bean.

Make the bean available for application use.

Spring Dependency Injection Principle

Dependency Injection (DI) injects required objects into a class via configuration or annotations. It is a concrete implementation of IoC.

DI diagram
DI diagram

Spring supports three injection methods:

Constructor Injection : inject dependencies through the class constructor.

Property (Setter) Injection : inject dependencies via setter methods.

Interface Injection : inject dependencies through an interface that the bean implements.

Spring AOP Principle

Aspect‑Oriented Programming (AOP) adds cross‑cutting concerns (e.g., logging, security, transactions) without modifying the original code. Spring AOP implements this via dynamic proxies.

Two proxy types are used:

JDK dynamic proxy – based on interfaces.

CGLIB proxy – based on subclassing concrete classes.

AOP proxy diagram
AOP proxy diagram

Typical AOP use cases include logging, performance monitoring, transaction management, and permission checks.

Spring Transaction Management

Spring supports programmatic and declarative transaction management. Declarative transactions are implemented using AOP; methods annotated with @Transactional are wrapped with a proxy that starts and commits/rolls back a database transaction.

Transaction flow diagram
Transaction flow diagram
@Transactional
public void insert(String userName) {
    this.jdbcTemplate.update("insert into t_user (name) values (?)", userName);
}

The essence of Spring transactions is the combination of AOP and the underlying database transaction mechanism, where the transaction logic is applied before and after the target method execution.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaAOPBackend DevelopmentSpringDependency InjectionTransaction Management

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion