스프링 핵심 원리 – 기본편


컴포넌트 스캔


컴포넌트 스캔과 의존관계 자동 주입

  • @ComponentScan : @Component가 붙은 모든 클래스를 스프링 빈을 모두 끌고 온다.
    • @ComponentScan.Filter(type = FilterType.ANNOTATION, classes =Configuration.class) 라는 식으로 빼고자 하는 빈을 뺀다.

image

  • @Autowired : 컴포넌트 스캔을 쓰면 오토 와이어드를 쓰게 된다. 왜냐하면 repository 타입에 맞는 아이를 찾아와서 의존관계 주입을 해준다.
    • getBean(MemberRepository.class) 이 코드와 동일하다.

컴포넌트 스캔의 대상

  • @Component: 컴포넌트 스캔
  • @Controller: mvc 컨트롤러
  • @Service: 비즈니스 로직
  • @Repository: 데이터 접근 계층
  • @Configuration: 스프링 설정 정보

image