0Pricing
Spring Boot 4 Microservices & REST APIs · Lektion

Der Spring-IoC-Container

Erfahren Sie, wie Spring Beans erstellt und verdrahtet.

Der Spring-IoC-Container ist eine kostenlose Spring Boot 4 Microservices & REST APIs-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Microservices & REST APIs-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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

Häufig gestellte Fragen

Ist die Lektion „Der Spring-IoC-Container“ kostenlos?

Ja — der vollständige Text von „Der Spring-IoC-Container“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Microservices & REST APIs-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Microservices & REST APIs-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Der Spring-IoC-Container“?

Erfahren Sie, wie Spring Beans erstellt und verdrahtet. Du übst Spring Boot 4 Microservices & REST APIs mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Spring Boot 4 Microservices & REST APIs zu starten?

Keine Vorkenntnisse erforderlich. Spring Boot 4 Microservices & REST APIs auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Der Spring-IoC-Container“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Spring Boot 4 Microservices & REST APIs-Lektion Code schreiben und ausführen?

Ja. Jede Spring Boot 4 Microservices & REST APIs-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Der Spring-IoC-Container
  2. Konstruktor- oder Feldinjektion
  3. Bean-Scopes
  4. Lifecycle-Callbacks
← Zurück zu Spring Boot 4 Microservices & REST APIs