Spring μ @Component λ±κ³Ό κ°μ νΉλ³ν annotation λ€μ λνμ¬ scan μ μννλ€. μ΄ν μλμΌλ‘ Spring Container μ μ΄λ€μ Bean μΌλ‘ λ±λ‘λλ€.
λ λ΄λΆμ μΌλ‘ λ€μ΄κ°λ³΄μ. Spring Framework μ λκ° scan μ μννλ κ²μΈκ°? κ·Έλ¦¬κ³ λκ° Bean μΌλ‘ λ±λ‘νλ κ²μΈκ°?
SpringBootApplication

Spring Initializr κ° μλμΌλ‘ μμ±ν΄μ€ Spring project μ Main Spring Boot Application class μ½λλ₯Ό μ΄ν΄λ³΄λ©΄ μμͺ½μ @SpringBootApplication annotation μ μ¬μ©νμκ³ , μ΄λ₯Ό μ¬μ©νκΈ° μνμ¬ ν΄λΉ package κ° import λ κ²μ μ μ μλ€.

λ΄λΆ ꡬνμΌλ‘ λ€μ΄κ°λ³΄λ©΄, @SpringBootApplication annotation μ μμ κ°μ΄ @EnableAutoConfiguration, @ComponentScan, @Configuration μ΄ ν¬ν¨λμ΄ μλ κ²μ λ³Ό μ μλ€. κ°κ°μ κΈ°λ₯μ λ€μκ³Ό κ°λ€.
@EnableAutoConfiguration: Spring Bootμμ classpath μ μλ λΌμ΄λΈλ¬λ¦¬λ₯Ό κΈ°λ°μΌλ‘ νμν μ€μ μ μλμΌλ‘ ꡬμ±@ComponentScan: μ§μ λ package μ νμ package λ₯Ό μ€μΊνμ¬Β@Component,Β@Service,Β@Controller,Β@RepositoryΒ λ±κ³Ό κ°μ annotation μ΄ λΆμ class λ₯Ό μλμΌλ‘ Bean Definition μ λ±λ‘@Configuration:@Beanannotation μ ν΅νμ¬ extra Beans λ₯Ό λ±λ‘νλ Bean μ μ class μμ λνλ
SpringApplication
μ΄λ²μλ SpringBootApplication μμ import λ SpringApplication μ΄λ€.
SpringApplication μ SpringApplication.run() μ Spring Boot Application μ Bootstrap μ μννλ€. μ¬κΈ°μΒ Bootstrap μ application μ μ€ν κ°λ₯ν μνλ‘ λ§λλΒ μ΄κΈ°ν λ° μ€λΉ κ³Όμ μ μλ―Ένλ€.
κ·ΈλΌ Bootstrap κ³Όμ μ μμλλ‘ μ΄ν΄λ³΄μ.
- SpringApplication.run() μ€ν
- SpringApplication κ°μ²΄ μμ±
- SpringApplication κ°μ²΄λ₯Ό μ΄κΈ°ννκ³ classpath μμ servlet κ΄λ ¨ library λ₯Ό νμΈ, servlet libraryt κ° μ‘΄μ¬νλ©΄ μΉ app, μμΌλ©΄ λΉμΉ app μΈμ§λ₯Ό κ°μ§
- νκ²½ μ€μ λ° νλ‘νμΌ μ μ©
application.propertiesΒ νμΌμ μ½μ΄ νκ²½ λ³μμ νλ‘νμΌμ μ€μ ApplicationEnvironmentPreparedEventλ₯Ό λ°μμμΌ νκ²½μ΄ μ€λΉλμμμ μλ¦Ό
- λ°°λ μΆλ ₯
- Spring Boot λ°°λ(κΈ°λ³Έ ASCII μνΈ)λ₯Ό μ½μμ μΆλ ₯νμ¬ application μμμ μλ¦Ό
- ApplicationContext μμ± λ° μ΄κΈ°ν
- ApplicationContext μμ±
- μΉ/λΉμΉ app μΈμ§μ λ°λΌμ Spring IoC Container μΈΒ
AnnotationConfigApplicationContextΒ λλΒAnnotationConfigServletWebServerApplicationContextλ₯Ό μμ±
- μΉ/λΉμΉ app μΈμ§μ λ°λΌμ Spring IoC Container μΈΒ
- Bean definition λ±λ‘ (Component Scan)
@ComponentScanμ μν΄ classpath μμ μ€μΊλ ν΄λμ€(@Component,Β@Service,Β@Controller,Β@Repository) λ₯Ό Bean definition μΌλ‘ λ±λ‘- μ€μ μ€μΊ μμ
μΒ
ClassPathBeanDefinitionScannerκ° μννλ©°, Bean definition μ context λ΄λΆμ Bean Factory μ μ μ₯ - default λ‘λ Main Spring Boot application μ΄ μ‘΄μ¬νλ package μλ§ scan λ²μκ° κ΅νλμ§λ§,
@SpringBootApplication( scanBasePackages={ ... } )λ₯Ό ν΅νμ¬ μΆκ° κ°λ₯
- ApplicationContext μμ±
- Auto-Configuration
@SpringBootApplicationμ@EnableAutoConfigurationμ μν΄ classpath μ library λ₯Ό κ°μ§νκ³ , νμν Bean κ³Ό μ€μ μ μλμΌλ‘ μΆκ° (λ°μ΄ν°λ² μ΄μ€ μ€μ , μΉ μλ² μ€μ λ±)
- λΉ μΈμ€ν΄μ€ν λ° μμ‘΄μ± μ£Όμ
:
- ApplicationContextλ λ±λ‘λ Bean definition μ κΈ°λ°μΌλ‘ μ€μ Bean object λ₯Ό μμ±
- DI λ₯Ό ν΅ν΄ Bean κ°μ κ΄κ³λ₯Ό μ€μ
- λ΄μ₯ μΉ μλ² μμ (μΉ μ ν리μΌμ΄μ
)
- classpath μ μΉ κ΄λ ¨ library κ° μμΌλ©΄ λ΄μ₯ Tomcat λ±μ μΉμλ²λ₯Ό μμ
- HTTP μμ²μ μ²λ¦¬ν μ€λΉλ₯Ό μλ£
- μ ν리μΌμ΄μ μ€ν μ€λΉ μλ£
μ§κΈκΉμ§ λμ¨ λ΄μ©μ μ€μ¬μΌλ‘ μμ½νλ©΄ λ€μκ³Ό κ°μ μμλ‘ μ§νλλ€.
SpringApplication μμ± > ApplicationContext μμ± > Component Scan > Bean Definition λ±λ‘ > Bean μμ±
Component Scanning Test

μμμ @ComponentScan μ ν΅ν Component Scanning μ κΈ°λ³Έμ μΌλ‘ Main Spring Boot Application μ΄ μ‘΄μ¬νλ package μλ§ κ΅νλλ€κ³ λ§νλ€. κ·Έλ¬λ νμ¬ μμ μν©μμλ, com.lucvs.util μ΄λΌλ λ€λ₯Έ package μλ§ @Component annotation μ΄ μ¬μ©λ class λ€μ΄ μ‘΄μ¬νκ³ μλ€.
μ΄λ‘ μ μΌλ‘λ μ λλ€λ κ²μ μμ§λ§, νλ² Application μ μ€νν΄λ³΄λ©΄ μ΄λ»κ² λ κΉ?

λ³΄ν΅ error λ κΈ°λΆμ΄ μ’μ§ μμ§λ§, μλλ error λ₯Ό λ°μμν¬ λμλ μ½κ°μ μΎκ°μ΄ μ‘΄μ¬νλ€. μμλ error κ° λ°μν κ²μ λ³Ό μ μλ€. error message λ₯Ό 보면 com.lucvs.util.Coach μ΄λΌλ type μ Bean μ μ°Ύμ μ μλ€κ³ νλ€.
λ°λΌμ, @SpringBootApplication annotation μ explicit νκ² base package λ€μ listing ν΄μ£Όμ΄μΌ νλ€.