Home/Learn/Spring Boot/Spring Framework & IoC

Spring Framework & IoC

Beginner
Core & Setup

The Spring Framework is built on Inversion of Control (IoC) — you declare what you need, the container creates and wires it. Understanding the IoC container is the foundation of everything else in Spring.

Overview

Inversion of Control means the framework, not your code, controls object creation and lifecycle. The ApplicationContext is the IoC container — it reads your configuration (annotations, XML, or Java), instantiates beans, resolves dependencies, and manages the full lifecycle from creation to destruction. Dependency Injection (DI) is the mechanism: instead of a class creating its collaborators with "new", it declares them as dependencies and the container injects them. This makes code testable, loosely coupled, and easy to swap implementations.

The IoC Container

BeanFactory is the basic IoC container. ApplicationContext extends it with AOP, event publishing, i18n, and eager bean initialization. In a Spring Boot app, SpringApplication.run() creates an AnnotationConfigServletWebServerApplicationContext — you rarely interact with it directly, but understanding it explains how everything else works.

Java — ApplicationContext bean lifecycle
// The container reads @Component, @Service, @Repository, @Controller, @Bean
// and builds a map of name → bean instance

// Conceptually what the container does:
// 1. Scan classpath for annotated classes (@ComponentScan)
// 2. Instantiate each bean (constructor or no-arg + setters)
// 3. Inject dependencies (constructor injection preferred)
// 4. Call @PostConstruct lifecycle methods
// 5. Register the bean, ready to serve

// Accessing the container directly (rarely needed):
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(App.class, args);

        // Look up a bean by type
        UserService userService = ctx.getBean(UserService.class);

        // Inspect registered bean names
        String[] names = ctx.getBeanDefinitionNames();
    }
}

Dependency Injection: Constructor vs Field vs Setter

Spring supports three DI styles. Constructor injection is the recommended approach: dependencies are final, the class is immutable, and it fails fast at startup if a dependency is missing. Field injection (@Autowired on a field) is convenient but hides dependencies and makes testing harder. Setter injection is for optional dependencies.

Java — three DI styles compared
// ✅ Constructor injection — recommended
@Service
public class OrderService {
    private final PaymentService paymentService;
    private final InventoryService inventoryService;

    // Spring calls this constructor and injects beans
    // @Autowired is optional when there is exactly one constructor
    public OrderService(PaymentService paymentService,
                        InventoryService inventoryService) {
        this.paymentService = paymentService;
        this.inventoryService = inventoryService;
    }
}

// ❌ Field injection — avoid (can't test without Spring)
@Service
public class OrderService {
    @Autowired private PaymentService paymentService; // hidden dependency
}

// ✅ Setter injection — for optional/changeable dependencies
@Service
public class NotificationService {
    private EmailClient emailClient;

    @Autowired(required = false) // optional — won't fail if bean missing
    public void setEmailClient(EmailClient emailClient) {
        this.emailClient = emailClient;
    }
}

Key Points to Remember

  • 1IoC container creates and wires objects — you declare dependencies, Spring fulfills them.
  • 2ApplicationContext is the full-featured IoC container used in Spring Boot apps.
  • 3Constructor injection is preferred: makes dependencies explicit, enables final fields, fails fast.
  • 4Field injection (@Autowired on fields) hides dependencies and breaks unit testing without Spring.
  • 5Beans are singletons by default — one instance shared across the entire application.
  • 6@PostConstruct and @PreDestroy handle bean lifecycle hooks.

Interview Questions

Sign in to ask Aria
1

What is Inversion of Control and how does Spring implement it?

EasyInfosys
2

Why is constructor injection preferred over field injection?

EasyTCS
3

What is the difference between BeanFactory and ApplicationContext?

MediumWipro
4

How does Spring resolve circular dependencies, and when does it fail?

HardAmazon
5

What happens if two beans implement the same interface and you @Autowire by type?

MediumRazorpay

Ask Aria about Spring Framework & IoC

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…