0Pricing
Spring Boot 4 Microservices & REST APIs · درس

حاوية Spring IoC

تعرّف إلى كيفية إنشاء Spring للحبوب وربطها

حاوية Spring IoC درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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

الأسئلة الشائعة

هل درس «حاوية Spring IoC» مجاني؟

نعم — نص درس «حاوية Spring IoC» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «حاوية Spring IoC»؟

تعرّف إلى كيفية إنشاء Spring للحبوب وربطها تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «حاوية Spring IoC»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. حاوية Spring IoC
  2. الحقن عبر المُنشئ مقابل الحقول
  3. نطاقات الحبوب
  4. استدعاءات دورة الحياة
← العودة إلى Spring Boot 4 Microservices & REST APIs