개발자가 직접 작성한 Class를 Bean으로 만드는 것이다.
@Component이 붙은 클래스는 @ComponentScan의 스캔 대상이 되며, 자동으로 Bean으로 등록된다.
@Configuration, @Controlller, @RestController, @Service, @Repository 어노테이션들은 모두 내부에 @Component를 가지고 있다.
@Component이 붙은 클래스를 스캔해서 Bean으로 등록한다.
Spring Boot 프로젝트의 시작 루트에 있는 ~Application.java 클래스에는 @SpringBootApplication 어노테이션이 붙어있는데, 이 내부에는 @ComponentScan이 포함되어 있다.
이로 인해 ~Application.java이 포함된 패키지와, 그 하위 패키지 모두가 컴포넌트 스캔 대상이 된다.
@Configuration
public class AppConfig {
@Bean
public MyService myService(){
ExternalLibrary externalLibrary = new ExternalLibrary();
externalLibrary.initialize();
return new MyService(externalLibrary);
}
}
myService 메서드에서 반환된MyService 객체는 Bean으로 등록된다.
@Component
public class SimpleService {
public String serve() {
return "Service is running";
}
}
Ref.
https://kimeck.tistory.com/10
https://park-algorithm.tistory.com/entry/Component란
https://withseungryu.tistory.com/64
https://code-lab1.tistory.com/170