[Spring] Component Scan And Auto wired Start

2021. 10. 6. 08:00ใ†Back-End ์ž‘์—…์‹ค/Spring Framework

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“‹ ๋ชฉ์ฐจ


01. [Spring] ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ
02. [Spring] ์ข‹์€ ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์˜ 5๊ฐ€์ง€ ์›์น™ (SOLID)
03. 
[Spring] ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์™€ ์Šคํ”„๋ง   
04. [Spring] OCP์™€ DIP ๊ณ ๋ คํ•˜๊ธฐ
05. [Spring] ๊ฐ์ฒด ์ง€ํ–ฅ ์›๋ฆฌ ์ ์šฉ - ์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ… ๊ฐœ๋ฐœ
06. 
[Spring] AppConfig ๋ฆฌํŒฉํ„ฐ๋ง    
07. [Spring] Ioc, DI Container  
08. [Spring] ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ  
09. [Spring] ์‹ฑ๊ธ€ํ†ค ์ปจํ…Œ์ด๋„ˆ  
10. [Spring] Component Scan And Auto wired Start
11. 
[Spring] ์˜์กด๊ด€๊ณ„ ์ฃผ์ž… ๋ฐฉ๋ฒ•    
12. [Spring] Bean Life Cycle Call Back
13. 
[Spring] Bean Scope

 

 

 

๐Ÿ“Œ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊ณผ ์˜์กด๊ด€๊ณ„ ์ž๋™ ์ฃผ์ž…


์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•  ๋•Œ ์ž๋ฐ” ์ฝ”๋“œ์˜ @Bean์ด๋‚˜, XML์˜ <bean> ๋“ฑ์„ ํ†ตํ•ด ์„ค์ • ์ •๋ณด์— ์ง์ ‘ ๋“ฑ๋กํ•  ์Šคํ”„๋ง ๋นˆ์„ ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด์—์š”. ํ•˜์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ๋“ฑ๋กํ•ด์•ผ ํ•  ์Šคํ”„๋ง ๋นˆ์ด ์ˆ˜์‹ญ, ์ˆ˜๋ฐฑ๊ฐœ๋ผ๋ฉด? ๋„˜๋‚˜ ํž˜๋“  ๊ฒƒ์ด์—์š”.

๊ฐœ๋ฐœ์ž๋Š” ๋ฐ˜๋ณต์„ ์‹ซ์–ดํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ธ ๊ฒƒ๋„ ์žˆ์–ด์š”!

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

๋˜, ์˜์กด๊ด€๊ณ„๋„ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•˜๋Š” @Autowired๋ผ๋Š” ๊ธฐ๋Šฅ๋„ ์žˆ๋‹ต๋‹ˆ๋‹ค!

 

         ๐Ÿ‘‰ AutoAppConfig.java ์ถ”๊ฐ€

 

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋จผ์ € Class ์„ ์–ธ์— @ComponentScan์„ ๋ถ™์—ฌ์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

์œ„์™€ ๊ฐ™์ด ํ•˜๋ฉด @Bean์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ต๋‹ˆ๋‹ค!

 

๐Ÿ’ก์ฐธ๊ณ  :
์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ์‚ฌ์šฉํ•˜๋ฉด @Configuration์ด ๋ถ™์€ ์„ค์ • ์ •๋ณด๋„ ์ž๋™์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.
excludeFilters๋Š” ์Šคํ”„๋ง ๋นˆ์— ๋“ฑ๋กํ•˜์ง€ ์•Š์„ ๋‚ด์šฉ์„ ์„ ์–ธํ•˜์—ฌ ์ œ์™ธ ์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์€ @Component๊ฐ€ ๋ถ™์€ Class๋ฅผ ์ฐพ์•„์„œ ์Šคํ”„๋ง ๋นˆ์— ๋“ฑ๋กํ•ด ์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

 

๐Ÿ’ก์ฐธ๊ณ  :
@Configuration ์—ญ์‹œ ํ•ด๋‹น ์†Œ์Šค์ฝ”๋“œ ๋‚ด๋ถ€๋ฅผ ์‚ดํŽด๋ณด๋ฉด @Component๊ฐ€ ๋ถ™์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.

 

์ปดํฌ๋„ŒํŠธ์˜ ์Šค์บ” ๋Œ€์ƒ์ด ๋˜๋„๋ก ํ•˜๊ณ  ์‹ถ์€ Class์—๋Š” @Component๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

         ๐Ÿ‘‰ MemoryMemberRepository.java

 

 

 

         ๐Ÿ‘‰ RateDiscountPolicy.java

 

 

 

         ๐Ÿ‘‰ MemberServiceImpl.java

 

 

  ์œ„์˜ Class์— ๋ชจ๋‘ @Component๋ฅผ ๋ถ™์—ฌ์ฃผ์—ˆ๊ณ , MemberServiceImpl์—๋Š” @Autowired๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

@Bean์„ ์‚ฌ์šฉํ•˜๋ฉด ์„ค์ • ์ •๋ณด๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ง์ ‘ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋ฅผ ํ†ตํ•ด ์˜์กด๊ด€๊ณ„๋„ ์ง์ ‘ ๋ช…์‹œํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

ํ•˜์ง€๋งŒ, ์œ„์˜ ๋ฐฉ๋ฒ•๋Œ€๋กœ ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์„ค์ • ์ •๋ณด ์ž์ฒด๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…๋„ ์ด ํด๋ž˜์Šค ์•ˆ์—์„œ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด @Autowired์ด๋ž๋‹ˆ๋‹ค!

 

         ๐Ÿ‘‰ OrderServiceImpl.java

 

 

@Autowired๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ƒ์„ฑ์ž์—์„œ ์—ฌ๋Ÿฌ ์˜์กด๊ด€๊ณ„๋„ ํ•œ๋ฒˆ์— ์ฃผ์ž… ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”!

 

 

         ๐Ÿ‘‰ AutoAppConfigTest.java

 

 

@Component๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ด๋„ AnnotaionConfigApplicationContext๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ @Bean์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ฒƒ์ด์—์š”. ์ด๋ฅผ ํ†ตํ•ด ์„ค์ • ์ •๋ณด๋กœ AutoAppConfig ํด๋ž˜์Šค๋ฅผ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค!

 

 

๐Ÿ“– Test ๊ฒฐ๊ณผ

 

 

์œ„์™€ ๊ฐ™์ด ์•„์ฃผ ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ต๋‹ˆ๋‹ค!

 

23:17:08.996 [Test worker] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
23:17:09.125 [Test worker] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\01.Synology_Sync\Person\97.source_code\00.study\00.Inflearn\์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ\build\classes\java\main\hello\core\discount\RateDiscountPolicy.class]
23:17:09.129 [Test worker] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\01.Synology_Sync\Person\97.source_code\00.study\00.Inflearn\์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ\build\classes\java\main\hello\core\member\MemberServiceImpl.class]
23:17:09.130 [Test worker] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\01.Synology_Sync\Person\97.source_code\00.study\00.Inflearn\์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ\build\classes\java\main\hello\core\member\MemoryMemberRepository.class]
23:17:09.132 [Test worker] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\01.Synology_Sync\Person\97.source_code\00.study\00.Inflearn\์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ\build\classes\java\main\hello\core\orderImsi\OrderServiceImpl.class]

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊ณผ ์ž๋™ ์˜์กด ๊ด€๊ณ„ ์ฃผ์ž…์ด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์ผ๊นŒ์š”?

 

 

         ๐Ÿ‘‰ @ComponentScan

 

 

@ComponentScan์€ @Component๊ฐ€ ๋ถ™์€ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ์ด์—์š”.

์ด ๋•Œ, ์Šคํ”„๋ง ๋นˆ์˜ ๊ธฐ๋ณธ ์ด๋ฆ„์€ ํด๋ž˜์Šค๋ช…์„ ์‚ฌ์šฉํ•˜๋˜, ๋งจ ์•ž ๊ธ€์ž๋งŒ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ๋“ฑ๋กํ•˜๋Š” ๊ฒƒ์ด์—์š”.

  • ๋นˆ ์ด๋ฆ„ ๊ธฐ๋ณธ ์ „๋žต : MemberServiceImpl Class => memberServiceImpl
  • ๋นˆ ์ด๋ฆ„ ์ง์ ‘ ์ง€์ • : ๋งŒ์•ฝ ์Šคํ”„๋ง ๋นˆ์˜ ์ด๋ฆ„์„ ์ง์ ‘ ์ง€์ •ํ•˜๊ณ  ์‹ถ์„ ์‹œ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉ
    • @Component("[Spring Bean Name]")

 

 

 

         ๐Ÿ‘‰ @Autowired ์˜์กด ๊ด€๊ณ„ ์ž๋™ ์ฃผ์ž…

 

 

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

์ด ๋•Œ, ๊ธฐ๋ณธ ์กฐํšŒ ์ „๋žต์€ ์ž๋ฃŒํ˜• ํƒ€์ž…(๊ฐ์ฒดํ˜•)์ด ๊ฐ™์€ ๋นˆ์„ ์ฐพ์•„์„œ ์ฃผ์ž…ํ•˜๋Š” ๊ฒƒ์ด์—์š”. 

getBean(MemberRepository.class)

์œ„์˜ ์ฝ”๋“œ์™€ ๋น„์Šทํ•˜๋‹ค๊ณ  ๋ณด๋ฉด ๋  ๊ฒƒ์ด์—์š”!

 

๋ฐ˜์‘ํ˜•

 

์ƒ์„ฑ์ž์— ๋งค๊ฐœ ๋ณ€์ˆ˜(ํŒŒ๋ผ๋ฏธํ„ฐ)๊ฐ€ ๋งŽ์•„๋„ ๋‹ค ์ฐพ์•„์„œ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

 

    ๐Ÿ“ ํƒ์ƒ‰ ์œ„์น˜์™€ ๊ธฐ๋ณธ ์Šค์บ” ๋Œ€์ƒ

         ๐Ÿ‘‰ ํƒ์ƒ‰ํ•  ํŒจํ‚ค์ง€์˜ ์‹œ์ž‘ ์œ„์น˜ ์ง€์ •

๋ชจ๋“  ์ž๋ฐ” ํด๋ž˜์Šค๋ฅผ ๋‹ค ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ํ•˜๊ฒŒ ๋˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ์•„๋งˆ ์—„์ฒญ๋‚œ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ฒŒ ๋  ๊ฒƒ์ด์—์š”. ๊ทธ๋ž˜์„œ ๊ผญ ํ•„์š”ํ•œ ์œ„์น˜๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋„๋ก ์„ค์ • ํ•ด ์ค„ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

 

@ComponentScan(
	basePackages = "hello.core",
}

 

์œ„์˜ ์ฝ”๋“œ์—์„œ basePackages๋Š” ํƒ์ƒ‰ํ•  ํŒจํ‚ค์ง€์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด์—์š”. ์ด ํŒจํ‚ค์ง€๋ฅผ ํฌํ•จํ•ด์„œ ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ, ๊ทธ ์™ธ์— ๋‚ด์šฉ๋“ค์€ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด์—์š”.

๋งŒ์•ฝ basePackages = {"hello.core", "hello.service"}์™€ ๊ฐ™์ด ํ•ด ์ฃผ์—ˆ๋‹ค๋ฉด core์™€ serviceํ•˜์œ„ ๋‚ด์šฉ๋“ค๋งŒ ๋ชจ๋‘ ํƒ์ƒ‰ํ•  ๊ฒƒ์ด์—์š”. ๋งŒ์•ฝ ์ด๊ฒƒ์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ฒŒ ๋˜๋ฉด @ComponentScan์ด ๋ถ™์€ ์„ค์ • ์ •๋ณด ํด๋ž˜์Šค์˜ ํŒจํ‚ค์ง€๊ฐ€ ์‹œ์ž‘ ์œ„์น˜๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

 

๊ถŒ์žฅ ์‚ฌํ•ญ

ํŒจํ‚ค์ง€ ์œ„์น˜๋ฅผ ์œ„์™€ ๊ฐ™์ด ๋”ฐ๋กœ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ , ์„ค์ • ์ •๋ณด ํด๋ž˜์Šค์˜ ์œ„์น˜๋ฅผ ํ”„๋กœ์ ํŠธ ์ตœ์ƒ๋‹จ์— ๋‘๊ฒŒ ๋œ๋‹ค๋ฉด ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ ๋‚ด์— ๋‚ด์šฉ๋“ค๋งŒ ์Šค์บ”์„ ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ์žฅํ•œ๋‹ค.

 

๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ๊ฐ€ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด?

 

  • com.hello
  • com.hello.services
  • com.hello.repository

 

com.hello => Project Start Root Package(Directory) ์ด ๊ณณ์— AppConfig๋“ฑ๊ณผ ๊ฐ™์€ Main Configuration ์ •๋ณด๋ฅผ ๋‘๊ณ , @ComponentScan์„ ๋ถ™์ด๊ณ , basePackges๋ฅผ ์ƒ๋žต

 

์œ„์˜ ๊ถŒ์žฅ์‚ฌํ•ญ๋Œ€๋กœ ํ•˜๊ฒŒ ๋˜๋ฉด com.hello๋ฅผ ํฌํ•จํ•œ ํ•˜์œ„ ๋ชจ๋“  ๋‚ด์šฉ์€ ์ž๋™์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ์ด ๋˜๋Š” ๊ฒƒ์ด์—์š”. ๊ทธ๋ฆฌ๊ณ , ํ”„๋กœ์ ํŠธ ๋ฉ”์ธ ์„ค์ • ์ •๋ณด๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ๋Œ€ํ‘œํ•˜๋Š” ์ •๋ณด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ƒ๋‹จ ์œ„์น˜(๋ฃจํŠธ)์— ๋‘๋Š” ๊ฒƒ์ด ์ข‹๋‹ต๋‹ˆ๋‹ค!

์ฐธ๊ณ ๋กœ ์Šคํ”„๋ง ๋ถ€ํŠธ ์‚ฌ์šฉ ์‹œ ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ๋Œ€ํ‘œ ์‹œ์ž‘ ์ •๋ณด์ธ @SpringBootApplication์„ ์ด ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ ๋ฃจํŠธ ์œ„์น˜์— ๋‘๋Š” ๊ฒƒ์ด ๊ด€๋ก€๋ผ๊ณ  ํ•˜๋„ค์š”! ์ด ์–ด๋…ธํ…Œ์ด์…˜ ์•ˆ์—๋Š” @ComponentScan์ด ๋“ค์–ด์žˆ๋‹ต๋‹ˆ๋‹ค!

 

 

         ๐Ÿ‘‰ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๊ธฐ๋ณธ ๋Œ€์ƒ

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์€ @Component์™€ ํ•จ๊ป˜ ์•„๋ž˜ ๋‚ด์šฉ๋„ ์ถ”๊ฐ€๋กœ ํฌํ•จ ๋˜๋Š” ๊ฒƒ์ด์—์š”.

  1. @Component : ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ์‹œ ์Šค์บ” ๋‹นํ•  Class ์„ ์–ธ
  2. @Controller : ์Šคํ”„๋ง MVC ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์‚ฌ์šฉ
  3. @Service : ์Šคํ”„๋ง ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์—์„œ ์‚ฌ์šฉ
  4. @Repository : ์Šคํ”„๋ง ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต์—์„œ ์‚ฌ์šฉ
  5. @Configuration : ์Šคํ”„๋ง ์„ค์ • ์ •๋ณด์—์„œ ์‚ฌ์šฉ

 

์œ„์˜ ํด๋ž˜์Šค์˜ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ์‚ดํŽด ๋ณด๋ฉด ๋ชจ๋‘ @Component๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

@Component public @interface Controller {
}

@Component public @interface Service {
}

@Component public @interface Configuration {
}

 

 

๐Ÿ’ก์ฐธ๊ณ  :
์–ด๋…ธํ…Œ์ด์…˜ ๊ฐ„์—๋Š” ์ƒ์† ๊ด€๊ณ„๋Š” ์กด์žฌํ•˜์ง€ ์•Š์€๋ฐ, ํŠน์ • ์–ด๋…ธํ…Œ์ด์…˜์„ ๋“ค๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์ž๋ฐ” ์–ธ์–ด์˜ ๊ธฐ๋Šฅ์ด ์•„๋‹ˆ๊ณ , ์Šคํ”„๋ง์ด ์ง€์›ํ•˜๋Š” ๊ธฐ๋Šฅ

 

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์˜ ์šฉ๋„ ๋ฟ ์•„๋‹ˆ๋ผ ๋‹ค์Œ ์–ด๋…ธํ…Œ์ด์…˜์ด ์žˆ์œผ๋ฉด ์Šคํ”„๋ง์€ ๋ถ€๊ฐ€ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

  1. @Controller : ์Šคํ”„๋ง MVC Controller๋กœ ์ธ์‹
  2. @Repository : ์Šคํ”„๋ง Data Access Layer๋กœ ์ธ์‹ํ•˜๊ณ , Data Layer์˜ Exception์„ ์Šคํ”„๋ง Exception์œผ๋กœ ๋ณ€ํ™˜
  3. @Configuration : ์Šคํ”„๋ง Configuration Infomation์„ ์ธ์‹ํ•˜๊ณ , ์Šคํ”„๋ง ๋นˆ์ด ์‹ฑ๊ธ€ํ†ค์„ ์œ ์ง€ํ•˜๋„๋ก ์ฒ˜๋ฆฌ
  4. @Service : ํŠน๋ณ„ํ•œ ๊ธฐ๋Šฅ์€ ์—†์œผ๋‚˜, ๊ฐœ๋ฐœ์ž๋“ค์ด ํ•ต์‹ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์—ฌ๊ธฐ์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

 

 

๐Ÿ’ก์ฐธ๊ณ  :
useDefaultFilters Option์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ผœ์ ธ ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์„ ๋„๊ฒŒ ๋˜๋ฉด ๊ธฐ๋ณธ ์Šค์บ” ๋Œ€์ƒ๋“ค ์ œ์™ธ
(๋‹จ์ˆœ ์ฐธ๊ณ  ์‚ฌํ•ญ)

 

 

 

๐Ÿ“Œ Filter


 

  1. includeFilters : ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ ์ถ”๊ฐ€ ์ง€์ •
  2. excludeFilters : ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ ์ œ์™ธ ์ง€์ •

 

      ๐Ÿ‘‰ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ ์ถ”๊ฐ€ ์–ด๋…ธํ…Œ์ด์…˜

 

 

      ๐Ÿ‘‰ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ ์ œ์™ธ ์–ด๋…ธํ…Œ์ด์…˜

 

 

 

      ๐Ÿ‘‰ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ ์ถ”๊ฐ€ํ•  ํด๋ž˜์Šค(@MyIncludeComponent ์ ์šฉ)

 

 

 

      ๐Ÿ‘‰ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ ์ œ์™ธํ•  ํด๋ž˜์Šค(@MyexcludeComponent ์ ์šฉ)

 

 

 

 

      ๐Ÿ‘‰ ์„ค์ • ์ •๋ณด์™€ ์ „์ฒด ํ…Œ์ŠคํŠธ ์ฝ”๋“œ

 

 

์œ„์— includeFilters์— MyIncludeComponent ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์„œ BeanA๊ฐ€ ๋“ฑ๋ก๋˜์—ˆ๊ณ , excludeFilters์— MyExludeComponent ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์„œ BeanB๋Š” ๋“ฑ๋ก๋˜์ง€ ์•Š๊ฒŒ ํ•œ ๊ฒƒ์ด์—์š”.

 

 

    ๐Ÿ“ FilterType Option

FilterType์—๋Š” 5๊ฐ€์ง€ Option์ด ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

  1. ANNOTION : Default, ์–ด๋…ธํ…Œ์ด์…˜์„ ์ธ์‹ํ•ด์„œ ๋™์ž‘
    • ์˜ˆ :) org.example.SomeAnntaion
  2. ASSIGNABLE_TYPE : ์ง€์ •ํ•œ Type๊ณผ ์ž์‹ Type์„ ์ธ์‹ํ•ด์„œ ๋™์ž‘
    • ์˜ˆ :) org.example.SomeClass
  3. ASPECTJ : AspectJ Pattern ์‚ฌ์šฉ
    • ์˜ˆ :) org.example..*Service+
  4. REGEX : ์ •๊ทœ ํ‘œํ˜„์‹ ์‚ฌ์šฉ
    • ์˜ˆ :) org\.example\.Default.*
  5. CUSTOM : TypeFilter ๋ผ๋Š” Interface๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์ฒ˜๋ฆฌ
    • ์˜ˆ :) org.example.MyTypeFilter

 

 

      ๐Ÿ‘‰ BeanA๋ฅผ ๋นผ๊ณ  ์‹ถ์„ ๋•Œ์˜ ์˜ˆ์‹œ ์ฝ”๋“œ

@ComponentScan(
 includeFilters = { @Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
 },
 
 excludeFilters = { @Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class),
 @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = BeanA.class)}
)

 

๐Ÿ’ก์ฐธ๊ณ  :
@Component๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์ถฉ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— includeFilters๋ฅผ ์‚ฌ์šฉํ•  ์ผ์€ ๊ฑฐ์˜ ์—†๋‹ค.
excludeFilters๋Š” ์—ฌ๋Ÿฌ ์ด์œ ๋กœ ๊ฐ„ํ˜น ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์œผ๋‚˜, ๋งŽ์ง€๋Š” ์•Š๋‹ค.
์ตœ๊ทผ ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š”๋ฐ, ์Šคํ”„๋ง ๊ธฐ๋ณธ ์„ค์ •์— ์ตœ๋Œ€ํ•œ ๋งž์ถ”์–ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

 

 

๐Ÿ“Œ ์ค‘๋ณต ๋“ฑ๋ก๊ณผ ์ถฉ๋Œ


์ปดํฌ๋„ŒํŠธ ์Šค์บ”์—์„œ ๊ฐ™์€ ๋นˆ ์ด๋ฆ„์„ ๋“ฑ๋กํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”? ๋‹ค์Œ ๋‘๊ฐ€์ง€ ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด์—์š”.

  1. ์ž๋™ ๋นˆ ๋“ฑ๋ก VS ์ž๋™ ๋นˆ ๋“ฑ๋ก
  2. ์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก VS ์ž๋™ ๋นˆ ๋“ฑ๋ก

 

 

    ๐Ÿ“  ์ž๋™ ๋นˆ ๋“ฑ๋ก VS ์ž๋™ ๋นˆ ๋“ฑ๋ก

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์Šคํ”„๋ง ๋นˆ์ด ๋“ฑ๋ก๋˜๋Š”๋ฐ, ๊ทธ ์ด๋ฆ„์ด ๊ฐ™์€ ๊ฒฝ์šฐ ์Šคํ”„๋ง์€ Exception์„ ๋ฐœ์ƒ ์‹œํ‚ค๋Š” ๊ฒƒ์ด์—์š”.

ConflictiongBeanDefinitionException ๋ฐœ์ƒ!

 

 

    ๐Ÿ“  ์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก VS ์ž๋™ ๋นˆ ๋“ฑ๋ก

์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก๊ณผ ์ž๋™ ๋นˆ ๋“ฑ๋ก์—์„œ ๋นˆ ์ด๋ฆ„์ด ์ถฉ๋Œ๋˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

 

@Component
public class MemoryMemberRepository implements MemberRepository {}

 

@Configuration
@ComponentScan( excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = Configuration.class))
public class AutoAppConfig {
 
 @Bean(name = "memoryMemberRepository")
 public MemberRepository memberRepository() {
 return new MemoryMemberRepository();
 }
}

 

์ด๋ ‡๊ฒŒ ๋  ๊ฒฝ์šฐ์—๋Š” ์ˆ˜๋™์œผ๋กœ ๋“ฑ๋กํ•œ ๋นˆ์ด ์šฐ์„ ๊ถŒ์„ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๊ณ , ์ˆ˜๋™ ๋นˆ์ด ์ž๋™ ๋นˆ์„ ์žฌ์ •์˜(Overriding)ํ•˜๋Š” ๊ฒƒ์ด์—์š”.

 

Overriding bean definition for bean 'memoryMemberRepository' with a different 
definition: replacing

 

์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก์‹œ ์œ„์™€ ๊ฐ™์€ Log๊ฐ€ ๋‚จ๊ฒŒ ๋œ๋‹ต๋‹ˆ๋‹ค!

 

๐Ÿ‘จ‍๐Ÿซ ๊น€์˜ํ•œ ๊ฐ•์‚ฌ๋‹˜์˜ ์กฐ์–ธ

๊ฐœ๋ฐœ์ž๊ฐ€ ์˜๋„์ ์œผ๋กœ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋Œ€ํ–ˆ๋‹ค๋ฉด ์ž๋™ ๋ณด๋‹ค๋Š” ์ˆ˜๋™์ด ์šฐ์„ ๊ถŒ์„ ๊ฐ€์ง€๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ, ํ˜„์‹ค์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์˜๋„์ ์œผ๋กœ ์„ค์ •ํ•ด์„œ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ธฐ ๋ณด๋‹ค ์—ฌ๋Ÿฌ ์„ค์ •๋“ค์ด ๊ผฌ์—ฌ ๊ฒฐ๊ณผ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ ‡๊ฒŒ  ๋˜๋ฉด ์ •๋ง ์žก๊ธฐ ์–ด๋ ค์šด Bug๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋˜๊ณ , ์ด๋Ÿฌํ•œ ์žก๊ธฐ ์–ด๋ ค์šด Bug๋Š” ํ•ญ์ƒ ์• ๋งคํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ  ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ๋Š” ์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก๊ณผ ์ž๋™ ๋นˆ ๋“ฑ๋ก์ด ์ถฉ๋Œ์ด ๋‚˜๋ฉด Exception์ด ํ„ฐ์ง€๋„๋ก ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•ด ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

 

Consider renaming one of the beans or enabling overriding by setting 
spring.main.allow-bean-definition-overriding=true

์œ„์˜ Exception์€ ์ˆ˜๋™ ๋นˆ๊ณผ ์ž๋™ ๋นˆ ๋“ฑ๋ก ์‹œ ์ถฉ๋Œ์ด ๋‚˜๋ฉด ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ํ„ฐํŠธ๋ฆฌ๋Š” ๋‚ด์šฉ์ธ ๊ฒƒ์ด์—์š”.

 

spring.main.allow-bean-definition-overriding=true

์œ„ ๋‚ด์šฉ์„ application.properties์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด Exception์ด ํ„ฐ์ง€์ง€ ์•Š๋Š”๋‹ต๋‹ˆ๋‹ค!

spring.main.allow-bean-definition-overriding=false

 

 

 

 


 

์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ธ€์ด ๋งˆ์Œ์— ๋“œ์…จ๋‚˜์š”? ๊ตฌ๋…๊ณผ ๊ณต๊ฐ! ๊ทธ๋ฆฌ๊ณ , ๋Œ“๊ธ€ ๊ทธ๋ฆฌ๊ณ  ๋ฐฉ๋ช…๋ก์€ ์ฃผ๋‹ˆํ•˜๋ž‘์—๊ฒŒ ๋งŽ์€ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•