Skip to content

Latest commit

ย 

History

History
90 lines (62 loc) ยท 5.35 KB

ComponentScan.md

File metadata and controls

90 lines (62 loc) ยท 5.35 KB

๐Ÿ“š ๋ณธ ๊ธ€์€ ์ธํ”„๋Ÿฐ ๊ฐ•์˜ '์Šคํ”„๋ง ํ•ต์‹ฌ ๊ธฐ์ˆ '์„ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.


1. @ComponentScan

Spring 3.1 ๋ถ€ํ„ฐ ๋„์ž…์ด ๋œ ๊ฒƒ์œผ๋กœ ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์„ ๋“ฑ๋กํ•˜๋ฉด XML ํŒŒ์ผ์— <context:component-scan> ์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ๋นˆ์„ ์ž๋™์œผ๋กœ ์Šค์บ”ํ•˜์—ฌ ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค.

  • @ComponentScan ์˜ ์—ญํ• 
    • ์Šค์บ” ์œ„์น˜ ์„ค์ •
    • ํ•„ํ„ฐ : ์–ด๋–ค ์–ด๋…ธํ…Œ์ด์…˜์„ ์Šค์บ”ํ• ์ง€ ๋˜๋Š” ์Šค์บ”ํ•˜์ง€ ์•Š์„์ง€ ์„ค์ •

๊ฐ€์žฅ ์ค‘์š”ํ•œ ์„ค์ •์€ basePackages , basePackageClasses ์„ค์ •๊ณผ @Filter๋ฅผ ํ†ตํ•ด ์Šค์บ”์„ ์ œ์™ธํ•  ๋Œ€์ƒ ์„ค์ •์ž…๋‹ˆ๋‹ค. ์šฐ์„  basePackages , basePackageClasses ๋Š” ์Šค์บ”ํ•  ์œ„์น˜๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

basePackages ๊ฐ’์€ String ์ด๊ธฐ ๋•Œ๋ฌธ์— ํŒจํ‚ค์ง€๊ฐ€ ๊ธด ๊ฒฝ์šฐ์—๋Š” ์ผ์ผ์ด ์ž…๋ ฅํ•˜๊ธฐ์—๋„ ๋ฒˆ๊ฑฐ๋กญ๊ธฐ๋„ ํ•˜๊ณ  String์œผ๋กœ ์ž…๋ ฅํ•˜๋‹ค๊ฐ€ ์˜คํƒ€๊ฐ€ ๋‚  ์ˆ˜๋„ ์žˆ๊ธฐ์— TypeSafe ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ TypeSafe ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” basePackageClasses ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค. basePackageClasses ๋Š” ์Šค์บ” ์œ„์น˜๋ฅผ ํด๋ž˜์Šค์˜ ์œ„์น˜๋กœ ๋ฐ›์•„ ์Šค์บ”์„ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

1.1 BasePackageClasses

@ComponentScan ์ด ๋ถ™์€ @Configuration์ด ๋ถ™์€ ๋Œ€์ƒ๋ถ€ํ„ฐ ์Šค์บ”์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

SpringBoot๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด, @SpringBootApplication ์–ด๋…ธํ…Œ์ด์…˜์— ๋“ค์–ด๊ฐ€๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {
        @Filter( type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class}), @Filter( type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(annotation = EnableAutoConfiguration.class)
    // ์ดํ•˜ ์ƒ๋žต...
}

@ComponentScan๊ณผ, @Configuration ์„ ๊ตฌํ˜„ํ•œ @SpringBootConfiguration์ด ๋ถ™์–ด์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— Application์ด ์‹œ์ž‘๋˜๋ฉด @SpringBootApplication์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ๊ธฐ์ ์œผ๋กœ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ํฌํ•จํ•˜๋Š” ํŒจํ‚ค์ง€์™€ ๊ทธ ์ดํ•˜์˜ ํด๋ž˜์Šค๋“ค, ๋˜๋Š” ํŒจํ‚ค์ง€๋“ค์ด ๋ชจ๋‘ ์Šค์บ” ๋Œ€์ƒ์ด ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ, @SpringBootApplication์ด ๋ถ™์€ ํด๋ž˜์Šค ๋ฐ–์˜ ํŒจํ‚ค์ง€๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ๊ทธ ํŒจํ‚ค์ง€๋Š” ์Šค์บ” ๋Œ€์ƒ์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ดํ•ด๋ฅผ ์œ„ํ•ด ์‚ฌ์ง„์œผ๋กœ ์„ค๋ช…๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์€ ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

image

๋นจ๊ฐ„์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ ํด๋ž˜์Šค๊ฐ€ @SpringBootApplication์ด ๋ถ™์€ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ dempspring51 ํŒจํ‚ค์ง€์— ์žˆ๋Š” ํด๋ž˜์Šค๋“ค์€ ์Šค์บ” ๋Œ€์ƒ์ด ๋˜์ง€๋งŒ, out ํŒจํ‚ค์ง€์— ์žˆ๋Š” ํด๋ž˜์Šค๋Š” ์Šค์บ” ๋Œ€์ƒ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

1.2 Filter

Filter๋Š” ์–ด๋–ค ์–ด๋…ธํ…Œ์ด์…˜์„ ์Šค์บ”ํ• ์ง€ ๋˜๋Š” ํ•˜์ง€ ์•Š์„์ง€๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์Šค์บ”์—์„œ ์ œ์™ธํ•  ๋Œ€์ƒ์ด ์žˆ๋‹ค๋ฉด excludeFilters ์—˜๋ฆฌ๋จผํŠธ์— @Filter ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 1.1 BasePackageClasses๋ฅผ ์„ค๋ช…ํ•˜๋ฉด์„œ ๋ณด์—ฌ๋“œ๋ ธ๋˜ @SpringBootApplication์— ๋ณด๋ฉด excludeFilters์— @Filter ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค ํด๋ž˜์Šค๋ฅผ ์ œ์™ธํ•  ์ง€ ์„ค์ •ํ•œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


2. @Component

@Component ๊ฐ€ ๋ถ™์€ ํด๋ž˜์Šค๋Š” @ComponentScan ์„ ํ†ตํ•ด ์ž๋™์œผ๋กœ ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋ฉ๋‹ˆ๋‹ค. ์ •ํ™•ํ•˜๊ฒŒ๋Š” @Component๊ฐ€ ๋ถ™์€ ํด๋ž˜์Šค ๋˜๋Š” @Component๋ฅผ ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๊ฐ–๊ณ  ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์„ ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๊ฐ€ ์ž๋™ ๋นˆ ๋“ฑ๋ก ๋Œ€์ƒ์ด ๋ฉ๋‹ˆ๋‹ค.

@Component๋ฅผ ๋ฉ”ํƒ€ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๊ฐ–๊ณ  ์žˆ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • @Controller
  • @Repository
  • @Service
  • @Configuration

3. @ComponentScan์˜ ๋™์ž‘์›๋ฆฌ

์‹ค์ œ ์Šค์บ๋‹์€ BeanFactoryPostProcessor์„ ๊ตฌํ˜„ํ•œ ConfigurationClassPostProcessor์— ์˜ํ•ด ์ฒ˜๋ฆฌ ๋ฉ๋‹ˆ๋‹ค.

BeanFactoryPostProcessor๋Š” BeanPostProcessor์™€ ๋น„์Šทํ•˜์ง€๋งŒ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. BeanFactoryPostProcessor๋Š” ๋‹ค๋ฅธ ๋ชจ๋“  ๋นˆ๋“ค์„ ๋งŒ๋“ค๊ธฐ ์ด์ „์— ์ ์šฉ์ด ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ฆ‰,@ComponentSacn์€ ๋‹ค๋ฅธ ๋นˆ๋“ค์„ ์ฐพ์•„ BeanFactoryPostProcessor๋ฅผ ๊ตฌํ˜„ํ•œ ConfigurationClassPostProcessor์— ์˜ํ•ด ๋นˆ์œผ๋กœ ๋“ฑ๋ก์ด ๋˜๋ฉฐ, @Autowired๋Š” ๋“ฑ๋ก๋œ ๋‹ค๋ฅธ ๋นˆ๋“ค์„ ์ฐพ์•„ BeanPostProcessor์„ ๊ตฌํ˜„ํ•œ AutowiredAnnotationBeanPostProcessor์— ์˜ํ•ด ์˜์กด์„ฑ ์ฃผ์ž…์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ๋นˆ๋“ค์€ @Controller, @Service, @Bean, @Respository ๋“ฑ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ” ์งš๊ณ  ๋„˜์–ด๊ฐ€๊ธฐ

  • ๊ทธ๋ ‡๋‹ค๋ฉด BeanFactoryPostProcessor -> BeanPostProcessor ์ˆœ์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ดํ•ดํ•ด๋„ ๋ ๊นŒ?
    • Yes

์ •๋ฆฌํ•˜์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • BeanPostProcessor
    • Spring Bean์˜ ์ƒ์„ฑ ์ „ํ›„์— Bean์— ๋Œ€ํ•œ ์ดˆ๊ธฐํ™” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
    • BeanPostProcessor๋Š” ๋นˆ(๋˜๋Š” ๊ฐ์ฒด) ์ธ์Šคํ„ด์Šค์ƒ์—์„œ ๋™์ž‘ํ•œ๋‹ค. ์ฆ‰, ์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ๋Š” ๋นˆ ์ธ์Šคํ„ด์Šค๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•œ ๋‹ค์Œ์— BeanPostProcessor๊ฐ€ ์ž์‹ ์˜ ์ผ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • BeanFactoryPostProcessor
    • Bean์˜ ์ •์˜ ์ž์ฒด๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. BeanPostProcessor๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰๋œ๋‹ค.