등록하는 법
- Component Scanning
- @Component
- @Controller
- @Service
- @Repository
- 직접 일일히 XML이나 자바 설정 파일에 등록
Component Scan
스프링부트로 만든 애플리케이션은 대부분 @SpringBootApplication이 붙은 부분이 있고 이 안에 @ComponentScan이 있다.
- 어노테이션은 자체는 기능이 없다. 주석과 같다.
- 어노테이션을 마커로 프로세스를 처리하는 애들이 있을 뿐이다.
모든 패키지에 @Component라는 어노테이션을 찾아서 빈으로 만들어 준다.
직접 등록
@SpringBootApplication
public class ..
@Bean
public String hello() {
return "hello";
}
- @Bean을 정의 할 때는 @Configuration 클래스 안에다 정의해야 한다.
- @SpringBootApplication 안에 포함되어 있다.
@Autowired
String hello;
System.out.println(hello); // hello
- 어떤 빈을 사용하고 싶을 때 사용할 클래스 또한 빈이어야 한다.
- 빈만이 빈을 사용할 수 있다.