[스프링 핵심 원리 - 기본편] 컴포넌트 스캔과 의존관계 자동 주입

Hyeonjun·2022년 9월 5일
0
post-thumbnail

컴포넌트 스캔과 의존관계 자동 주입 시작하기

  • 지금까지 스프링 빈을 등록할 때는 자바 코드의 @Bean이나 XML의 등을 통해서 설정 정보에 직접 등록할 스프링 빈을 나열했다.
  • 예제에서는 몇 개 되지 않지만, 이렇게 등록해야 할 스프링 빈이 수십, 수백개가 되면 일일이 등록하기도 귀찮고, 설정 정보도 커지고, 누락하는 문제도 발생한다.
    • 역시 개발자는 반복을 싫어한다.
    • 무엇보다 귀찮다.
  • 그래서 스프링은 설정 정보가 없어도 자동으로 스프링 빈을 등록하는 컴포넌트 스캔이라는 기능을 제공한다.
  • 또, 의존관계도 자동으로 주입하는 @Autowired라는 기능도 제공한다.

AutoAppConfig

@Configuration
@ComponentScan(
        excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
)
public class AutoAppConfig {
    
}
  • 컴포넌트 스캔을 사용하려면 먼저 @ComponentScan을 설정 정보에 붙여주면 된다.
  • 기존의 AppConfig와는 다르게 @Bean으로 등록한 클래스가 하나도 없다.

컴포넌트 스캔을 사용하면 @Configuration이 붙은 설정 정보도 자동으로 등록되기 때문에, AppConfig, TestConfig 등 앞서 만들어두었던 설정 정보도 함께 등록되고, 실행되어 버린다.

그래서 excludeFilters를 이용해서 설정정보는 컴포넌트 스캔 대상에서 제외했다.

보통 설정 정보를 컴포넌트 스캔 대상에서 제외하진 않지만, 기존 예제 코드를 남기고 유지하기 위해 사용한다.

  • 컴포넌트 스캔은 이름 그대로 @Component 어노테이션이 붙은 클래스를 스캔해서 스프링 빈으로 등록한다. @Component를 붙여주자.

@Configuration이 컴포넌트 스캔의 대상이 된 이유도 @Configuration소스코드를 여렁보면 @Component 어노테이션이 붙어있기 때문이다.

구현체에 @Component 추가

MemoryMemberRepository

@Component
public class MemoryMemberRepository implements MemberRepository {
	...
}

RateDiscountPolicy

@Component
public class RateDiscountPolicy implements DiscountPolicy{
	...
}

MemberServiceImpl

@Component
public class MemberServiceImpl implements MemberService{

    private final MemberRepository memberRepository;

    @Autowired
    public MemberServiceImpl(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }
	
		...
}
  • 이전에 AppConfig에서는 @Bean으로 직접 설정 정보를 작성했고, 의존관계도 직접 명시했다.
    • 이제는 이런 설정정보 자체가 없기 때문에, 의존관계 주입도 이 클래스 안에서 해결해야 한다.
  • @Autowired는 의존관계를 자동으로 주입해준다.

OrderServiceImpl

@Component
public class OrderServiceImpl implements OrderService{

    private final MemberRepository memberRepository;
    private final DiscountPolicy discountPolicy;

    @Autowired
    public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
        this.memberRepository = memberRepository;
        this.discountPolicy = discountPolicy;
    }

		...
}
  • @Autowired를 사용하면 생성자에서 여러 의존관계도 한번에 주입받을 수 있다.

AutoAppConfigTest

public class AutoAppConfigTest {

    @Test
    void basicScan() {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);

        MemberService memberService = ac.getBean(MemberService.class);
        assertThat(memberService).isInstanceOf(MemberService.class);
    }
}
  • AnnotationConfigApplicationContext를 사용하는 것은 기존과 동일
  • 설정 정보로 AutoAppConfig클래스를 넘겨준다.
  • 실행해보면 기존과 같이 잘 동작하는 것을 확인할 수 있다.

실행결과

13:44:42.031 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [C:\workspace\Study\Spring_core\out\production\classes\hello\core\discount\RateDiscountPolicy.class]
13:44:42.033 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [C:\workspace\Study\Spring_core\out\production\classes\hello\core\member\repository\MemoryMemberRepository.class]
13:44:42.035 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [C:\workspace\Study\Spring_core\out\production\classes\hello\core\member\service\MemberServiceImpl.class]
13:44:42.037 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [C:\workspace\Study\Spring_core\out\production\classes\hello\core\order\service\OrderServiceImpl.class]

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

1. @ComponentScan

  • @ComponentScan@Component가 붙은 모든 클래스를 스프링 빈으로 등록한다.
    • 당연히 싱글톤으로 저장함
  • 이때 스프링 빈의 기본 이름은 클래스명을 사용하되 맨 앞글자만 소문자를 사용한다.
    • 빈 이름 기본 전략: MemberServiceImpl → memberServiceImpl
    • 빈 이름 직접 지정: 만약 스프링 빈의 이름을 직접 지정하고 싶으면 @Component("{이름}")과 같이 부여할 수 있다.

2. @Autowired 의존관계 자동 주입

  • 생성자에 @Autowired를 지정하면, 스프링 컨테이너가 자동으로 해당 스프링 빈을 찾아서 주입한다.
  • 이때 기본 조회 전략은 타입이 같은 빈을 찾아서 주입한다.
    • getBean(MemberRepository.class)와 동일하다고 이해하면 된다.

  • 생성자에 파라미터가 많아도 다 찾아서 자동으로 주입한다.
profile
더 나은 성취

0개의 댓글