Spring IoC Container
Spring'in bean'leri nasıl oluşturup birbirine bağladığını öğrenin.
Spring IoC Container, CoddyKit'te ücretsiz bir Spring Boot 4 Microservices & REST APIs dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Spring Boot 4 Microservices & REST APIs öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Boot 4 Microservices & REST APIs kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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
ApplicationContextmanages beans @Component+ scanning registers your classes;@Beanhandles others- Injection resolves by type, disambiguated with
@Primary/@Qualifier
Sıkça Sorulan Sorular
“Spring IoC Container” dersi ücretsiz mi?
Evet — “Spring IoC Container” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Spring Boot 4 Microservices & REST APIs kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Boot 4 Microservices & REST APIs kursu toplamda 4 dersten oluşur.
“Spring IoC Container” dersinde ne öğreneceğim?
Spring'in bean'leri nasıl oluşturup birbirine bağladığını öğrenin. Spring Boot 4 Microservices & REST APIs ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Spring Boot 4 Microservices & REST APIs öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Spring Boot 4 Microservices & REST APIs, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“Spring IoC Container” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Spring Boot 4 Microservices & REST APIs dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Spring Boot 4 Microservices & REST APIs dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Spring IoC Container
- Yapıcı ve Alan Enjeksiyonu
- Bean Kapsamları
- Yaşam Döngüsü Geri Çağırmaları