0Pricing
Spring Boot 4 Microservices & REST APIs · Lesson

The Spring IoC Container

How Spring creates and wires beans.

The Spring IoC Container is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Inversion of Control

Inversion of Control (IoC) means your objects no longer create their own collaborators. Instead, a container constructs them and hands over (injects) their dependencies.

This flips the traditional flow: rather than calling new everywhere, you declare what you need and let the framework wire it up.

The ApplicationContext

Spring’s IoC container is the ApplicationContext. It reads bean definitions, instantiates them, resolves dependencies, and manages their lifecycle from creation to shutdown.

In Spring Boot, SpringApplication.run builds this context for you and keeps it running.

What Is a Bean?

A bean is simply an object that the container creates and manages. The context knows how to build it, what it depends on, and when to destroy it.

Beans are typically singletons living for the life of the application, though other scopes exist.

Declaring Beans with @Component

Annotate a class with @Component and component scanning will register it. Specializations like @Service, @Repository, and @Controller are @Component with added semantics.

@Service
public class OrderService {
    public void placeOrder() { /* ... */ }
}

@Repository
public class OrderRepository { }

Component Scanning

@SpringBootApplication includes @ComponentScan, which searches the main class’s package and sub-packages for stereotype annotations. Keep your classes under that root package so they are discovered.

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Declaring Beans with @Bean

For types you do not own or want to configure manually, define a factory method annotated @Bean inside a @Configuration class. The return value becomes a managed bean.

@Configuration
public class AppConfig {
    @Bean
    public RestClient restClient() {
        return RestClient.builder()
            .baseUrl("https://api.example.com")
            .build();
    }
}

@Component vs @Bean

Both create beans, but differ in control:

  • @Component — you own the class and let scanning register it
  • @Bean — explicit factory method, ideal for third-party classes or conditional construction

Dependency Injection in Action

The container reads a bean’s constructor parameters and supplies matching beans. Here OrderService receives a fully built OrderRepository automatically.

@Service
public class OrderService {
    private final OrderRepository repo;
    public OrderService(OrderRepository repo) {
        this.repo = repo;
    }
}

Resolving by Type

Spring injects by type. If exactly one bean matches the parameter type, it is wired in. The bean’s concrete class can implement an interface, and you inject the interface.

@Service
public class NotificationService {
    private final MessageSender sender; // interface
    public NotificationService(MessageSender sender) {
        this.sender = sender; // gets the single impl bean
    }
}

Handling Multiple Candidates

When several beans match a type, disambiguate with @Primary on the default, or @Qualifier at the injection point to name the exact one.

@Bean @Primary
MessageSender emailSender() { return new EmailSender(); }

@Bean
MessageSender smsSender() { return new SmsSender(); }

// inject a specific one
public Svc(@Qualifier("smsSender") MessageSender s) { }

Why IoC Helps

Delegating wiring to the container brings real benefits:

  • Testability — inject mocks instead of real collaborators
  • Loose coupling — depend on interfaces, not constructors
  • Centralized lifecycle — one place manages creation and shutdown

Quick Check

Test your understanding of bean declaration.

Recap

The IoC container wires your application together.

  • IoC means the container, not your code, creates and injects collaborators
  • The ApplicationContext manages beans
  • @Component + scanning registers your classes; @Bean handles others
  • Injection resolves by type, disambiguated with @Primary/@Qualifier

Frequently asked questions

Is the “The Spring IoC Container” lesson free?

Yes — the full text of “The Spring IoC Container” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.

What will I learn in “The Spring IoC Container”?

How Spring creates and wires beans. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Microservices & REST APIs?

No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Spring IoC Container” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?

Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Spring IoC Container
  2. Constructor vs Field Injection
  3. Bean Scopes
  4. Lifecycle Callbacks
← Back to Spring Boot 4 Microservices & REST APIs