Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 9

    Beans & IoC Container

    A Spring bean is any object Spring manages.

    Course progress0%
    Focus
    3 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    Introduction

    A Spring bean is any object Spring manages. The IoC (Inversion of Control) container is the registry that creates beans, injects them, manages their lifecycle and tears them down. Inversion of control means you don't control object creation — Spring does.

    Understanding the topic

    How beans are registered:

    • @Component — generic bean.
    • @Service, @Repository, @Controller — semantic flavours of @Component.
    • @Bean on a method inside @Configuration — manual registration (use for 3rd-party classes).
    • Scopes: singleton (default), prototype, request, session.

    Informative example

    Bean from a 3rd-party library:

    ts
    @Configuration
    public class HttpClientConfig {
    @Bean
    public RestClient stripeClient(@Value("${stripe.api-key}") String key) {
    return RestClient.builder()
    .baseUrl("https://api.stripe.com")
    .defaultHeader("Authorization", "Bearer " + key)
    .build();
    }
    }
    Ready to mark this lesson complete?Track your journey across the entire course.