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.