Post

Bean with Component Scan(2)

πŸ’‘ How to add Bean

  • @Beanλ“±λ‘ν•˜λŠ” 방법
  • 1️⃣ manual bean configuration with @Configuration
  • 2️⃣ component scan with @ComponentScan, @Autowired
  • 이 κ²Œμ‹œκΈ€μ—μ„œλŠ” 2οΈβƒ£λ²ˆ 방법에 λŒ€ν•΄ μ„€λͺ…

βœ… Component Scan

  • Spring does not magically know what to add as bean ➑️ scans your project marked as bean
  • this scanning is called: @Component Scan

  • @Component Scan은 @Component μ–΄λ…Έν…Œμ΄μ…˜μ΄ 뢙은 ν΄λž˜μŠ€λŠ” λͺ¨λ‘ Spring Bean으둜 μ•Œμ•„μ„œ λ“±λ‘ν•œλ‹€.
    • @Component
    • @Controller
    • @Service
    • @Repository
    • @Configuration
  • μ˜μ‘΄μ„± μ£Όμž…: @Autowired
  • @Autowiredκ°€ 뢙은 μƒμ„±μžλŠ” μžλ™μœΌλ‘œ ν•΄λ‹Ή Spring Bean을 μ°Ύμ•„μ„œ μ˜μ‘΄μ„±μ„ μ£Όμž…ν•œλ‹€.

  • 1️⃣ (before)manual bean configuration
  • Bean으둜 λ“±λ‘ν•˜κ³  싢은 ν΄λž˜μŠ€λ“€μ„ AppConfig에 λͺ…μ‹œν•΄μ•Ό ν–ˆμŒ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
public class AppConfig {

    @Bean
    public MemberService memberService(){
        return new MemberServiceImpl(memberRepository());
    }

    @Bean
    public OrderService orderService(){
        return new OrderServiceImpl(
                memberRepository(),
                discountPolicy());
    }
}
  • 2️⃣ component scan with @ComponentScan
  • 이제 config피일은 ν……ν…… λΉ„μ—ˆλ‹€!
  • @ComponentScan은 @Componentκ°€ 뢙은 클래슀λ₯Ό μ°Ύμ•„ spring bean으둜 λ“±λ‘ν•œλ‹€.
1
2
3
@Configuration
@ComponentScan
public class AutoAppConfig {}
  • 그리고 bean으둜 λ“±λ‘ν•˜κ³  싢은 ꡬ체 ν΄λž˜μŠ€μ— @Componentλ₯Ό 뢙인닀.
  • 그리고 @Autowiredκ°€ 뢙은 λ©”μ†Œλ“œλ₯Ό μžλ™μœΌλ‘œ μ˜μ‘΄μ„±μ„ μ£Όμž…ν•œλ‹€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Component //μžλ™μœΌλ‘œ spring bean등둝
public class OrderServiceImpl implements OrderService {
    private final MemberRepository repository;
    private final DiscountPolicy discountPolicy;

    @Autowired //μžλ™μœΌλ‘œ μ˜μ‘΄μ„± μ£Όμž…
    public OrderServiceImpl(MemberRepository repository, DiscountPolicy discountPolicy) {
        this.repository = repository;
    }

@Component  //μžλ™μœΌλ‘œ spring bean등둝
public class MemberServiceImpl implements MemberService{
    private final MemberRepository memberRepository;

    @Autowired //μžλ™μœΌλ‘œ μ˜μ‘΄μ„± μ£Όμž…
    public MemberServiceImpl(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }
}

@Component //μžλ™μœΌλ‘œ spring bean등둝
public class OrderServiceImpl implements OrderService {
}

βœ… Component Scan λ²”μœ„ μ§€μ •ν•˜κΈ°

  • place config file in root of project file
  • basePackages: search below this package
  • excludeFilters: do not add this class as bean
1
2
3
4
5
6
@Configuration
@ComponentScan(
        basePackages = "com.example.spring_basic",
        excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class )
)
public class AutoAppConfig { }

βœ… Filter

  • filter what/what not to include in component scan
  • includeFilters
  • excludeFilters
1
2
3
4
5
6
@Configuration
@ComponentScan(
        includeFilters =  @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
        excludeFilters =  @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class))
static class ComponentFilterAppConfig{
    }

❓ Bean duplication, bean collision

  • ❓ What happens if bean names are identical?
  • μ»΄ν¬λ„ŒνŠΈ μŠ€μΊ”μ— μ˜ν•΄ μžλ™μœΌλ‘œ μŠ€ν”„λ§ 빈이 λ“±λ‘λ˜λŠ”λ°, κ·Έ 이름이 같은 경우 μŠ€ν”„λ§μ€ 였λ₯˜ λ°œμƒ μ‹œν‚΄
  • πŸ”΄ ConflictingBeanDefinitionException μ˜ˆμ™Έ λ°œμƒ

  • ❓ What happens if bean is added both manually and auto with component scan?
  • There are two ways of adding bean
  • 1️⃣ manual bean configuration with @Configuration
  • 2️⃣ component scan with @ComponentScan
  • 두 방법 λͺ¨λ‘μœΌλ‘œ 같은 bean이 두 번 λ“±λ‘λœλ‹€λ©΄?

  • 1️⃣ manual bean configuration has priority
  • manual bean overrides component scan
  • πŸ”΄ κ·ΈλŸ¬λ‚˜ ꢌμž₯ν•˜λŠ” 방법이 μ•„λ‹ˆλ©°, SpringbootλŠ” μ΅œκ·Όμ— 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚€λ„λ‘ λ°”λ€œ

βœ…

βœ…

βœ…

βœ…

βœ…

This post is licensed under CC BY 4.0 by the author.