Spring is one of the most widely used Java frameworks, but understanding Spring Boot becomes much easier when we first understand what's happening underneath the hood.

Today, I revisited some important Spring Core concepts, starting with configuration and dependency injection, and gradually moving towards bean lifecycle, proxies, AOP, and transaction management.

Here's what I learned.

1. Spring Configuration: Annotation-Based vs Java-Based

Spring provides different ways to configure and manage application beans.

Annotation-Based Configuration

In this approach, we use annotations to tell Spring which classes it should manage.

Some commonly used annotations are:

  • @Component – Marks a class as a Spring-managed component.
  • @Service – Indicates a service-layer component.
  • @Repository – Indicates a persistence-layer component.
  • @Controller – Defines a Spring MVC controller.
  • @Autowired – Requests dependency injection.
  • @Qualifier – Specifies which bean should be injected when multiple candidates exist.
  • @ComponentScan – Tells Spring where to search for components.

Example:

@Component public class PaymentService { public void pay() { System.out.println("Payment processing"); } } 

Java-Based Configuration

Java-based configuration uses @Configuration and @Bean to explicitly define beans.

@Configuration public class AppConfig { @Bean public PaymentService paymentService() { return new PaymentService(); } } 

This approach is particularly useful when working with third-party classes that we cannot modify to add Spring annotations.

Key takeaway: Annotation-based configuration allows Spring to discover components, while Java-based configuration gives us explicit control over bean creation.

2. IoC Container and Dependency Injection

One of the fundamental principles of Spring is Inversion of Control (IoC).

Traditionally, a class might create its own dependencies using the new keyword.

Spring changes this approach.

Instead of objects managing their own dependencies, the Spring IoC container creates and manages objects and supplies their dependencies.

These Spring-managed objects are called beans.

The two important container interfaces are:

  • BeanFactory – Provides fundamental IoC and dependency injection functionality.
  • ApplicationContext – Extends BeanFactory and provides additional capabilities such as event publishing, internationalization, and resource loading.

Example:

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); PaymentService paymentService = context.getBean(PaymentService.class); 

The container manages the bean rather than requiring us to instantiate it manually.

3. Eager and Lazy Initialization

Spring singleton beans are eagerly initialized by default.

This means Spring generally creates singleton beans during application context initialization.

However, we can defer bean creation using @Lazy.

@Component @Lazy public class PaymentService { public PaymentService() { System.out.println("PaymentService initialized"); } } 

The bean is created when it is first requested.

An interesting scenario occurs when an eager singleton depends on a lazy bean.

@Component public class OrderService { @Autowired private PaymentService paymentService; } 

Even though PaymentService is marked @Lazy, Spring needs to resolve it while creating OrderService.

Therefore, the dependency can still be instantiated during application startup.

We can also use @Lazy at an injection point to inject a lazy-resolution proxy.

Key takeaway: @Lazy delays instantiation, not bean registration.

4. Understanding Spring Bean Scopes

Bean scope determines how Spring creates and shares bean instances.

The important scopes are:

Scope Behaviour
Singleton One instance per Spring IoC container
Prototype A new instance for each bean request
Request One instance per HTTP request
Session One instance per HTTP session
Application One instance per ServletContext

Singleton is the default scope.

An interesting scenario: Prototype inside Singleton

Consider:

@Component @Scope("prototype") public class PaymentService { } 
@Component public class OrderService { @Autowired private PaymentService paymentService; public PaymentService getPaymentService() { return paymentService; } } 

Even though PaymentService has prototype scope, injecting it directly into a singleton OrderService does not create a fresh instance every time the getter is called.

Why?

Because Spring creates the singleton OrderService once and injects the prototype dependency during its creation.

The same reference is then returned by the getter.

To obtain a fresh prototype instance whenever needed, we can use ObjectProvider<PaymentService> and call getObject().

This was an important distinction between bean scope and normal Java object references.

5. Spring Bean Lifecycle

Spring manages beans throughout their lifecycle.

The major stages are:

  1. Instantiation – Spring creates the bean.
  2. Dependency Injection – Spring injects its dependencies.
  3. Aware callbacks – Applicable awareness interfaces are invoked.
  4. Before Initialization – BeanPostProcessor executes.
  5. Initialization – Methods such as @PostConstruct execute.
  6. After Initialization – BeanPostProcessor executes again.
  7. Bean Ready for Use – The bean is available to the application.
  8. Destruction – Spring invokes applicable destruction callbacks when the container shuts down.

Example:

@Component public class UserService { public UserService() { System.out.println("Constructor"); } @PostConstruct public void init() { System.out.println("Initialized"); } @PreDestroy public void destroy() { System.out.println("Destroyed"); } } 

The constructor runs first, followed by dependency injection and initialization callbacks.

When the application context closes, Spring invokes the destruction callback for managed singleton beans.

An important detail: Spring does not automatically invoke destruction callbacks for prototype beans.

6. BeanPostProcessor

A BeanPostProcessor allows us to customize beans during their lifecycle.

It provides two important methods:

public interface BeanPostProcessor { Object postProcessBeforeInitialization( Object bean, String beanName); Object postProcessAfterInitialization( Object bean, String beanName); } 

The first method executes before initialization callbacks such as @PostConstruct.

The second executes after initialization callbacks.

The execution sequence is:

Constructor → Dependency Injection → Before Initialization → @PostConstruct → After Initialization

Why is this important?

Spring uses infrastructure involving BeanPostProcessors to implement several advanced features, including the creation of proxies for eligible beans.

7. Understanding Proxies in Spring

A proxy is an intermediary object that controls access to another object.

Instead of directly invoking the actual service object, our application can invoke a proxy.

The proxy can execute additional logic before or after forwarding the method call.

For example:

OrderService | v PaymentService Proxy | |-- Logging |-- Security checks |-- Transaction handling | v Actual PaymentService 

Spring commonly uses two types of proxies:

  • JDK Dynamic Proxy – Uses Java interfaces.
  • CGLIB Proxy – Creates a subclass of the target class.

Proxies are an important foundation for understanding Spring AOP.

8. Spring AOP and Transaction Management

Aspect-Oriented Programming (AOP) helps separate cross-cutting concerns from business logic.

Examples include:

  • Logging
  • Security
  • Transaction management
  • Caching

Consider a money transfer operation:

@Service public class AccountService { @Transactional public void transferMoney() { debitAccount(); creditAccount(); } } 

We don't need to manually write transaction-handling code inside every business method.

Spring's transaction infrastructure can manage the transaction through a proxy.

The flow is:

  1. A method call reaches the transaction proxy.
  2. Spring starts a transaction.
  3. The actual business method executes.
  4. If successful, Spring commits the transaction.
  5. If an applicable exception occurs, Spring rolls back the transaction.

The database executes the SQL operations and enforces transactional guarantees, while Spring manages transaction boundaries.

What happens when something fails?

Suppose debitAccount() succeeds but creditAccount() throws a runtime exception.

If both operations participate in the same transaction, Spring rolls back the transaction.

This prevents the debit from being committed independently.

By default, Spring rolls back on unchecked exceptions (RuntimeException) and Error, but not on checked exceptions. We can customize this using rollbackFor and noRollbackFor.

9. What I Took Away From Today's Learning

The most valuable part of today's revision was understanding how these concepts connect.

They are not isolated Spring features:

  • IoC provides control over object creation.
  • Dependency Injection connects application components.
  • Bean scopes determine object instance management.
  • Bean lifecycle defines how Spring initializes and destroys beans.
  • BeanPostProcessor provides lifecycle extension points.
  • Proxies intercept method calls.
  • AOP uses interception to implement cross-cutting concerns.
  • Transaction management uses these mechanisms to manage database transactions.

Understanding these fundamentals makes Spring Boot much less mysterious.

Instead of simply memorizing annotations, I want to understand what Spring actually does behind the scenes.

The goal is not just to write Spring Boot applications, but to understand how they work internally.