Backend Development 9 min read

Understanding Spring IoC, Bean Instantiation, and Dependency Injection

This article explains the concepts of coupling, Inversion of Control, and Dependency Injection in the Spring framework, demonstrates various bean instantiation methods—including no‑arg constructors, static and instance factory methods—and provides a complete example of configuring and using Spring beans in a Java application.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Spring IoC, Bean Instantiation, and Dependency Injection

In the introductory section, the author reflects on developing Java SE/OOP projects without frameworks, highlighting the tight coupling caused by manual object creation and how Spring's IoC container reduces this coupling.

1. Coupling

The author advises writing native OOP projects before learning Spring to fully appreciate layer and class relationships.

2. Spring

Spring is described as a lightweight Java SE/EE framework whose core is based on Inversion of Control (IoC) and Aspect‑Oriented Programming (AOP).

3. Core IoC Understanding

IoC reverses control by delegating object creation to the Spring container, which still maintains dependencies but reduces direct coupling.

1. Container

The container manages beans (objects) defined in XML or via annotations, storing them in a map keyed by id.

During startup, Spring reads bean definitions, creates objects via reflection, and registers them for later injection using autowired , resource , or XML ref attributes.

2. Control Inversion

Before IoC, a class must manually instantiate its dependencies; after IoC, the container creates and injects them, turning the dependency acquisition from active to passive.

3. Dependency Injection

DI is the concrete implementation of IoC, where the container injects required dependencies into objects at runtime.

4. Bean Instantiation

Examples of creating beans:

1. No‑arg constructor

<bean id="userDao" class="yu7daily.dao.impl.UserDaoImpl"/>

2. Static factory method

<bean id="userDao" class="yu7daily.factory.StaticFactory" factory-method="getUserDao"/>
public class StaticFactory {
    public static UserDao getUserDao() { return new UserDaoImpl(); }
}

3. Instance factory method (common)

<bean id="factory" class="yu7daily.factory.DynamicFactory"/>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"/>
public class DynamicFactory {
    public UserDao getUserDao() { return new UserDaoImpl(); }
}

5. Bean Dependency Injection

DI injects persistence‑layer objects into service‑layer objects, reducing coupling.

1. Setter injection

Define a setter method in the target class and configure the bean with a p:b-ref="b" attribute.

private B b;
public void setB(B b) { this.b = b; }

2. Constructor injection

private B b;
public A(B b) { this.b = b; }

6. First Spring Example

The example walks through adding Spring dependencies, defining interfaces and implementations, creating an XML configuration file, declaring beans for BookDaoImpl and BookServiceImpl , and retrieving them via ApplicationContext .

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao1");
bookDao.save();
BookService bookService = (BookService) ctx.getBean("bookService");
bookService.save();

The execution result is shown as an image in the original article.

BackendJavaIoCSpringdependency injectionBean
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.