Master Spring’s Core: Quick Guide to IOC Container, Bean Scopes & Lifecycle

This concise guide explains Spring’s core concepts—including the Inversion of Control container, how beans are created and managed, the different bean scopes, and the full bean lifecycle with key extension points—providing clear analogies and practical configuration examples.

Ray's Galactic Tech
Ray's Galactic Tech
Ray's Galactic Tech
Master Spring’s Core: Quick Guide to IOC Container, Bean Scopes & Lifecycle

IOC Container (Inversion of Control)

The core idea is control inversion: instead of developers instantiating objects with new and managing dependencies, the Spring container creates, assembles, and manages objects, and developers only declare dependencies. An analogy is a food‑delivery platform where you place an order and the service delivers the dish.

Traditional way: developer uses new to create objects and manually handles dependencies.

IOC way: Spring container handles object creation, wiring, and lifecycle; developers simply declare what they need.

What is the container?

The container is essentially a sophisticated HashMap that stores bean definitions and instances. There are two main implementations:

BeanFactory – the most basic container providing full IOC functionality; supports lazy loading, creating a bean only when getBean() is called, which is useful in resource‑constrained scenarios.

ApplicationContext – the enterprise‑level container used in most applications; it eagerly creates all singleton beans at startup (eager loading), and adds features such as internationalization, event publishing, and AOP integration. This is the “Spring container” most developers refer to.

Core responsibilities of the container

Instantiation: creates objects via reflection.

Assembly: injects dependencies through setter injection or constructor injection.

Management: manages the full bean lifecycle and supplies beans to callers on demand.

Bean Scopes

A bean’s scope determines how long its instance lives inside the container.

singleton (default): one instance per container; typical for stateless utilities, services, and DAOs.

prototype: a new instance is created each time the bean is requested; useful for stateful objects such as request DTOs.

request: one instance per HTTP request; ideal for request‑scoped data in web layers.

session: one instance per user session; used for login state, shopping‑cart data, etc.

application: one instance for the entire web application; suitable for global shared data similar to ServletContext.

Configuring bean scope

XML: <bean id="..." class="..." scope="prototype"/> Annotation:

@Scope("prototype")

Bean Lifecycle

A bean progresses from creation to destruction when the container shuts down.

Construction: object is created via its constructor.

Dependency injection: Spring injects required properties.

Aware callbacks: the bean receives container and its own metadata (e.g., BeanNameAware).

Pre‑initialization processing: BeanPostProcessor.postProcessBeforeInitialization is invoked.

Initialization: custom init logic runs – @PostConstruct, afterPropertiesSet() (from InitializingBean), or a user‑defined init‑method.

Post‑initialization processing: BeanPostProcessor.postProcessAfterInitialization is invoked.

Ready: the bean is fully initialized and can be used.

Destruction (on container close): cleanup hooks run – @PreDestroy, destroy() (from DisposableBean), or a custom destroy‑method.

Mnemonic: Construction → Injection → Aware → Pre‑process → Initialization → Post‑process → Ready → Destruction.

Quick reference

IOC Container: creates, assembles, and manages beans; ApplicationContext is the most commonly used implementation. Analogy: a food‑delivery platform that takes orders (dependency declarations) and delivers dishes (bean instances).

Bean Scope: defines a bean’s lifespan—singleton, prototype, request, session, or application. Analogy: global pot versus disposable chopsticks.

Bean Lifecycle: full birth‑to‑death process with multiple extension points for custom logic. Analogy: a person’s life from birth, growth, usage, to death.

Bean lifecycle diagram
Bean lifecycle diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaIoCspringbean-lifecycleBean Scope
Ray's Galactic Tech
Written by

Ray's Galactic Tech

Practice together, never alone. We cover programming languages, development tools, learning methods, and pitfall notes. We simplify complex topics, guiding you from beginner to advanced. Weekly practical content—let's grow together!

0 followers
Reader feedback

How this landed with the community

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.