@Configuration
public class AppConfig {
@Bean
public SimpleBean simpleBean() {
// 최초 한 번만 실행됨
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
// simpleBean()을 호출해도, 프록시가 기존 싱글톤을 반환함
return new SimpleBeanConsumer(simpleBean());
}
simpleBeanConsumer는 simpleBean를 의존성으로 갖고 있음.@Configuration@Bean을 사용하는 클래스의 경우 @Configuration을 사용하는 것을 권장함.빈(Bean)으로 관리하라는 의미.@Configuration이 사용된 클래스는 CGLIB 프록시를 기반으로 빈(Bean)을 싱글톤으로 유지함.이 점이 @Component와의 가장 큰 차이점.싱글톤 빈(Bean)이 생성됨.빈(Bean)을 반환해줌.@Configuration이 사용된 클래스 내부에 정의된 @Bean 메서드를 호출할 때 스프링 컨테이너에 해당 빈이 이미 존재하는지 확인하고 존재 한다면 그 빈을 반환해줌.@Component, @Service, @Repository, @Controller 등 @Configuration을 제외한 모든 스테레오타입 어노테이션은 Lite 모드로 작동함.@Component // 또는 @Service, @Repository 등
public class AppConfig {
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer(SimpleBean simpleBean) { // ⭐ 의존성을 인수로 주입받음
// 스프링이 컨테이너의 싱글톤 SimpleBean을 인수로 넣어줌
return new SimpleBeanConsumer(simpleBean);
}
// 스프링의 의존성 주입(DI)을 통해 싱글톤 유지.
@Component
public class MyConfig {
@Bean
public ServiceA aService() {
return new ServiceA();
}
@Bean
public ServiceB bService() {
return new ServiceB(aService());
}
}
/*
bService() 내부의 aService() 호출은
스프링 컨테이너에서 관리되는 빈이 아니라, 그냥 new ServiceA()를 다시 만들어 반환함.
즉, ServiceA가 2번 생성되는 것. (싱글톤 깨짐)
*/
@Component
빈(Bean)으로 등록하고자 하는 경우 @Component를 활용.컴포넌트(Component)라는 의미.빈(Bean) 등록해줌.즉, @Bean 메서드를 호출할 때는 싱글톤을 보장하지만, @Bean 메서드가 또 다른 @Bean 메서드를 호출할 때는 싱글톤이 아닌 새로운 객체를 반환함.
Lite 모드를 사용할 때는 빈 간의 의존성 주입이 필요한 경우 메서드 인수를 통해 주입받아야 함.
@Component의 기능을 @Configuration(proxyBeanMethods = false)로 동일하게 설정할 수 있음.
(이러한 방식을 Bean 경량 모드 처리(bean light mode processing)라고 부르기도 함)
@Component
public static class Config {
@Autowired
SimpleBean simpleBean;
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
@Bean
public SimpleBeanConsumer simpleBeanConsumer() {
return new SimpleBeanConsumer(simpleBean);
}
}
| - | @Component | @Configuration |
|---|---|---|
| 목적 및 역할 | - 일반 빈(Bean) 등록용 클래스. - 일반적인 컴포넌트. - 클래스 자체만 빈으로 등록됨. | - 빈(Bean) 등록 및 설정용 클래스. - 설정 클래스(여러 Bean을 정의하는 클래스) 등록. - @Bean 메서드를 통해 여러 빈을 등록 가능. |
| 빈(Bean) 생성 방식 | - 클래스 자체를 빈(Bean)으로 등록. | - @Bean 메서드를 통해 빈(Bean) 등록. |
| 프록시 사용 | - 사용 X. | - CGLIB 프록시 사용 (싱글톤 보장). |
| 중복 호출 시 | - 새로운 객체를 반환할 수 있음. | - 같은 객체를 반환함. (싱글톤 유지) |
@Configuration
@Component 계열