[Spring Framework] Component Scan

Component Scan

์Šคํ”„๋ง์€ ์„ค์ • ์ •๋ณด๊ฐ€ ์—†์–ด๋„ ์ž๋™์œผ๋กœ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต ์˜์กด๊ด€๊ณ„๋„ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•˜๋Š” @Autowired๋ผ๋Š” ๊ธฐ๋Šฅ๋„ ์ œ๊ณต

package hello.core;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.FilterType;
  import static org.springframework.context.annotation.ComponentScan.*;
  
  @Configuration
  @ComponentScan(
  excludeFilters = @Filter(type = FilterType.ANNOTATION, classes =
    Configuration.class))
    public class AutoAppConfig {
}
  • ์ปดํฌํ„ดํŠธ ์Šค์บ”์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋จผ์ € @ComponentScan์„ ์„ค์ • ์ •๋ณด์— ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค.
  • ๊ธฐ์กด์˜ AppConfig์™€ ๋‹ค๋ฅด๊ฒŒ @Bean์œผ๋กœ ๋“ฑ๋กํ•œ ํด๋ž˜์Šค๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋‹ค.

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์€ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ @Component์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ์Šค์บ”ํ•ด์„œ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค. @Component๋ฅผ ๋ถ™์—ฌ์ฃผ์ž.

  • ์˜ˆ์‹œ
   @Component
   public class MemberServiceImpl implements MemberService {
      
      private final MemberRepository memberRepository;
      
      @Autowired
      public MemberServiceImpl(MemberRepository memberRepository) {
          this.memberRepository = memberRepository;
      }
}
  • Appconfig์—์„œ๋Š” @Bean์œผ๋กœ ์ง์ ‘ ์„ค์ • ์ •๋ณด๋ฅผ ์ž‘์„ฑํ–ˆ๊ณ , ์˜์กด๊ด€๊ณ„๋„ ์ง์ ‘ ๋ช…์‹œํ–ˆ๋‹ค. ํ˜„์žฌ๋Š” ์ด๋Ÿฐ ์„ค์ • ์ •๋ณด ์ž์ฒด๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…๋„ ํด๋ž˜์Šค ์•ˆ์—์„œ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.
  • @Autowired๋Š” ์˜์กด๊ด€๊ณ„๋ฅผ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•ด์ค€๋‹ค.
@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๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ƒ์„ฑ์ž์—์„œ ์—ฌ๋Ÿฌ ์˜์กด๊ด€๊ณ„๋„ ํ•œ๋ฒˆ์— ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
  import hello.core.AutoAppConfig;
  import hello.core.member.MemberService;
  import org.junit.jupiter.api.Test;
  import org.springframework.context.ApplicationContext;
  import
  org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import static org.assertj.core.api.Assertions.*;
  
  public class AutoAppConfigTest {
      
      @Test
      void basicScan() {
      ApplicationContext ac = new
  AnnotationConfigApplicationContext(AutoAppConfig.class);
          
          MemberService memberService = ac.getBean(MemberService.class);
          assertThat(memberService).isInstanceOf(MemberService.class);
      
      }
}

  • AnnotationConfigApplicationContext๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๊ธฐ์กด๊ณผ ๋™์ผํ•˜๋‹ค.
  • ์„ค์ • ์ •๋ณด๋กœ AutoConfigํด๋ž˜์Šค๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.

๊ฒฐ๊ณผ

ClassPathBeanDefinitionScanner - Identified candidate component class:
  .. RateDiscountPolicy.class
  .. MemberServiceImpl.class
  .. MemoryMemberRepository.class
  .. OrderServiceImpl.class

  • @ComponentScan ์€ @Component ๊ฐ€ ๋ถ™์€ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.
  • ์ด๋•Œ ์Šคํ”„๋ง ๋นˆ์˜ ๊ธฐ๋ณธ ์ด๋ฆ„์€ ํด๋ž˜์Šค๋ช…์„ ์‚ฌ์šฉํ•˜๋˜ ๋งจ ์•ž๊ธ€์ž๋งŒ ์†Œ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
    • ๋นˆ ์ด๋ฆ„ ๊ธฐ๋ณธ ์ „๋žต: MemberServiceImpl ํด๋ž˜์Šค memberServiceImpl
    • ๋นˆ ์ด๋ฆ„ ์ง์ ‘ ์ง€์ •: ๋งŒ์•ฝ ์Šคํ”„๋ง ๋นˆ์˜ ์ด๋ฆ„์„ ์ง์ ‘ ์ง€์ •ํ•˜๊ณ  ์‹ถ์œผ๋ฉด @Component("memberService2") ์ด๋Ÿฐ์‹์œผ๋กœ ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜๋ฉด ๋œ๋‹ค.

  • ์ƒ์„ฑ์ž์— @Autowired๋ฅผ ์ง€์ •ํ•˜๋ฉด, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž๋™์œผ๋กœ ํ•ด๋‹น ์Šคํ”„๋ง ๋นˆ์„ ์ฐพ์•„์„œ ์ฃผ์ž…ํ•œ๋‹ค.
  • ์ด๋•Œ ๊ธฐ๋ณธ ์กฐํšŒ ์ „๋žต์€ ํƒ€์ž…์ด ๊ฐ™์€ ๋นˆ์„ ์ฐพ์•„์„œ ์ฃผ์ž…ํ•œ๋‹ค.
    • getBean(MemberRepository.class) ์™€ ๋™์ผํ•˜๋‹ค๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.

์ฐธ์กฐ : Spring ํ•ต์‹ฌ์›๋ฆฌ

Categories:

Updated:

Leave a comment