0Pricing
Spring Boot 4 Microservices & REST APIs · 강의

Spring IoC 컨테이너

Spring이 빈을 만들고 연결하는 방식을 알아보세요.

Spring IoC 컨테이너은(는) CoddyKit의 무료 Spring Boot 4 Microservices & REST APIs 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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 AI 튜터), CoddyKit PRO로 업그레이드하면 Spring Boot 4 Microservices & REST APIs 강의 전체를 잠금 해제할 수 있습니다. Spring Boot 4 Microservices & REST APIs 강의에는 총 4개의 강의가 포함되어 있습니다.

“Spring IoC 컨테이너”에서 뭘 배우나요?

Spring이 빈을 만들고 연결하는 방식을 알아보세요. 브라우저에서 직접 실행하는 실습 코드로 Spring Boot 4 Microservices & REST APIs을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Spring Boot 4 Microservices & REST APIs을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Spring Boot 4 Microservices & REST APIs은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“Spring IoC 컨테이너” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Spring Boot 4 Microservices & REST APIs 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Spring Boot 4 Microservices & REST APIs 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. Spring IoC 컨테이너
  2. 생성자 주입과 필드 주입
  3. 빈 범위
  4. 수명 주기 콜백
← Spring Boot 4 Microservices & REST APIs(으)로 돌아가기