@Configuration
어노테이션이 붙은 클래스는 Spring의 애플리케이션 컨텍스트에 의해 빈 정의를 위한 설정 클래스로 인식됨.@Bean
메서드를 통해 Spring IoC 컨테이너에 빈을 등록할 수 있음.@Bean
어노테이션이 붙은 메서드는 Spring에 의해 호출되어 반환된 객체가 애플리케이션 컨텍스트에 빈으로 등록됨import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
@Bean
public AnotherService anotherService() {
return new AnotherService();
}
}
AppConfig
클래스는 @Configuration
어노테이션이 붙어 있으며, 두 개의 빈 myService
와 anotherService
를 정의하고 있음. 이 빈들은 Spring IoC 컨테이너에 의해 관리됨
@Bean
메서드로 생성된 빈은 싱글턴임.@Configuration
클래스는 Spring의 애플리케이션 컨텍스트에 의해 관리되며, 애플리케이션의 설정 및 관리에 필요한 정보를 제공@Configuration
클래스는 종종 @ComponentScan
, @EnableAutoConfiguration
, @PropertySource
등 다른 설정 관련 어노테이션과 함께 사용되어, 설정을 보다 구체적으로 조정할 수 있음.@Profile
어노테이션을 사용하여 특정 환경에서만 활성화되는 빈을 정의할 수 있음