티스토리 뷰

/** https://devlog-wjdrbs96.tistory.com/167?category=882236 그대로 복사했습니다*/

1. ComponentScan

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

여기서 @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 {

그러면 위와 같은 어노테이션들이 안에 담겨 있다는 것을 알 수 있는데 보면 ComponentScan이라는 어노테이션도 존재한다. 따라서 DemoApplicaion이라는 파일과 같은 패키지 or 하위패키지들 중에서 빈으로 등록할 수 있는 것들을 찾아서 빈으로 등록해준다. DemoApplication과 다른 패키지라면 빈으로 등록되지 않기 때문에 참고하길 바란다. 

 

그러면 ComponentScan은 빈을 등록하고자 할 때 어떤 것을 찾을까? 

  • @Component
    • @Repository
    • @Sevice
    • @Controller
    • @Configuration
    • @RestController

 

위와 같은 어노테이션들이 붙은 것들을 찾아 빈으로 등록하게 된다. Service, Repository, Componet 등의 차이가 무엇인지는 여기를 읽어보자.

 

그리고 실제 ComponentScan의 실제 스캐닝은ConfigurationClassPostProcessor라는 BeanFactoryPostProcessor에 의해 처리된다. 

 

 

 

 

@Configuration이란?

@Configuration 어노테이션이 붙어 있으면 스프링 IoC 컨테이너에게 해당 클래스는 Bean 설정 파일이라는 것을 알려주는 뜻이다. 그리고 위의 DemoApplication 클래스에 Main 메소드를 봐보자.

SpringApplication.run(DemoApplication.class, args);

여기서 run() 메소드에 첫 번째로 넘겨줘야 할 인자는 config 클래스이다. config 클래스란 @Configuration 어노테이션이 붙은 클래스를 의미한다. 위에서 본 것 처럼 @SpringBootApplication 안에 @Configuration이 있기 때문에 DemoApplication 클래스도 config 클래스에 해당한다. 이러한 파일을 ApplicationContext가 읽어 실행하게 된다.

 

 

 

 

# 인프런 강의 (백기선 / 스프링 프레임워크 핵심 기술)

IoC 컨테이너 1부: 스프링 IoC 컨테이너와 빈 https://joinwithyou.tistory.com/24

IoC 컨테이너 2부: ApplicationContext와 다양한 빈 설정 방법 https://joinwithyou.tistory.com/25

IoC 컨테이너 3부: @Autowire https://joinwithyou.tistory.com/26

IoC 컨테이너 4부: @Component와 컴포넌트 스캔 https://joinwithyou.tistory.com/27

IoC 컨테이너 5부: 빈의 스코프 https://joinwithyou.tistory.com/28

IoC 컨테이너 6부: Environment 1부. 프로파일 https://joinwithyou.tistory.com/29

IoC 컨테이너 6부: Environment 2부. 프로퍼티 https://joinwithyou.tistory.com/30

IoC 컨테이너 7부: MessageSource https://joinwithyou.tistory.com/31

IoC 컨테이너 8부: ApplicationEventPublisher https://joinwithyou.tistory.com/32

IoC 컨테이너 9부: ResourceLoader https://joinwithyou.tistory.com/33

 

# Reference

https://devlog-wjdrbs96.tistory.com/165?category=882236 (블로그)

https://www.inflearn.com/course/spring-framework_core (백기선 강의)

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함