0. Intro
이 글은 Spring 공식 문서 'Core Technologies'의 'IoC Container'에 대하여 정리한 글입니다.
+ 토비의 스프링 스터디에서 동일한 내용에 대한 PR 링크입니다.
1. IoC Container : ApplicatoinContext
org.springframework.context.ApplicationContext interface는 Spring IoC Container를 나타내며, Bean을 인스턴스화, 구성(configuring)하는 일을 담당한다. 또 BeanFactory의 하위 인터페이스로, 몇가지 enterprise-specific functionality의 추가적인 기능을 제공한다.
container는 configuration metadata를 읽음으로써 인스턴스화, 구성 및 어셈블할 구성 요소에 대한 지침을 얻는다.
configuration metadata는 annotated component class, factory method가 있는 configuration class, 외부 XML 파일 또는 Groovy script로 표현될 수 있다.
어떤 형식을 사용하든 애플리케이션과 해당 components 간의 풍부한 상호 의존성을 구성할 수 있다.
2. Configuration Metadata
스프링 IoC 컨테이터는 configuraion meatadata를 사용하여 애플리케이션의 객체를 인스턴스화, 구성(configuration), 결합한다.
configuration metadata 형식은 아래 3가지가 있다.
1. XML
2. Annotation-based Configuration
3. Java-based Configuration (주로 사용되는 방법)
하나씩 살펴보자
2-1. XML
<!-- src/main/resources/applicationContext.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Bean 정의 -->
<bean id="myBean" class="com.example.MyBean">
<property name="name" value="Spring XML Config" />
</bean>
</beans>
설정이 명확하게 분리되어 있어 비즈니스 로직과 설정이 혼동되지 않지만, 비교적 복잡하다.
2-2. Annotation-based Configuration
// com/example/MyBean.java
package com.example;
import org.springframework.stereotype.Component;
@Component
public class MyBean {
private String name;
// Setter
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
간결하고 명시적이지만, 비즈니스 로직과 설정이 혼합될 수 있다.
2-3. Java-based Configuration (주로 사용되는 방법)
// com/example/AppConfig.java
package com.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
MyBean myBean = new MyBean();
myBean.setName("Spring Java Config");
return myBean;
}
}
Java-based Configuration은 일반적으로 @Configuration 클래스 내에서 @Bean 어노테이션이 달린 메서드를 사용한다.
강력한 타입 안정성을 제공하며, IDE의 지원을 받을 수 있다.
3. Bean : BeanDefinition
컨테이너 내부적으로 빈에 대한 정의는 BeanDefinition 객체(Bean Configuration Metadata)로 표현된다.


BeanDefinition은 아래 4가지 metadata를 담고 있다.
- A package-qualified class name : 일반적으로 정의되는 빈의 실제 구현 클래스
- 컨테이너에서 빈이 어떻게 동작해야 하는지를 나타내는 빈 동작 구성 요소 (scope, lifecycle callback 등)
- 빈이 작업을 수행하는 데 필요한 다른 빈에 대한 참조 (dependencies)
- 새로 만든 개체에서 설정할 기타 구성
이러한 데이터는 각 빈 정의를 구성하는 일련의 properties(속성)로 변환된다.
(Class, Name, Scope, Constructor arguments, Properties, Autowiring mode, Lazy initialization mode, Initialization method, Destruction method)
4. 정리
IoC 컨테이너인 'ApplicationContext'가 Bean에 대한 정의를 담고있는 'BeanDefinition'을 통해 Bean을 생성하고 관리한다. 모든 Configuration Metadata(xml 기반, annotation 기반, Java 기반)는 BeanDefinition 인터페이스로 추상화되어 사용된다. (역할과 구현이 나뉘었으며, '@Bean', '<bean>' 빈 하나당 BeanDefinition 하나가 생성된다.)
'java & spring' 카테고리의 다른 글
[Spring Security] csrf().disable()하는 이유는?! 그리고 CORS (3) | 2024.11.03 |
---|---|
[Design Pattern] 스프링은 어떤 디자인 패턴을 사용하고 있을까? (0) | 2024.11.02 |
[Spring] 싱글톤 레지스트리와 스프링의 IoC 컨테이너 (0) | 2024.08.20 |
[Java/Spring] Java Bean vs Spring Bean, Bean이란? (0) | 2024.08.08 |