@SpringBootApplication의 동작 방식
@SpringBootApplication의 동작 방식
Spring-Boot의 Main Application 코드에는 @SpringBootApplication이라는 Annotation이 존재한다.
1 |
|
- @SpringBootApplication
1 |
|
이 Annotation의 주된 내용은 아래 3가지 이다.
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan
@SpringBootConfiguration
- @SpringBootConfiguration은 기존 @Configuration과 마찬가지로 해당 클래스가 @Bean 메서드를 정의되어 있음을 Spring 컨테이너에 알려주는 역할을 한다.
- @Configuration 어노테이션의 대안으로 둘의 차이점은 아래와 같이 설명하고 있다.
@SpringBootConfiguration is an alternative to the @Configuration annotation. The main difference is that @SpringBootConfiguration allows configuration to be automatically located. This can be especially useful for unit or integration tests.
@EnableAutoConfiguration
- @ComponentScan에서 먼저 스캔해서 Bean으로 등록하고 tomcat등 스프링이 정의한 외부 의존성을 갖는 class들을 스캔해서 Bean으로 등록한다.
- 이 때 정의된 class들은 spring-boot-autoconfigure안에 있는 MATE-INF 폴더에 spring.factories라는 파일안에 정의되어 있다.
- 이 중에서 spring.boot.enableautoconfiguration을 key로하는 외부 의존성 class들이 존재한다.
- 여기에 정의된 모든 class를 가져오는 게 아니라 class에 내부에 정의된 어노테이션에 따라 그 조건에 부합하는 class들만 생성된다.
- @ConditionalOnProperty, @ConditionalOnClass, @ConditionalOnBean 등등
1 | public EnableAutoConfiguration { |
spring.factories 위치


1 | ... |
번외. 내장 Tomcat Class
spring.factories 파일 안에 보면
1 | org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\ |
가 있고 이 class의 코드를 보면 아래와 같이 tomcatServletWebServerFactoryCustomizer() 메서드를 통해 tomcat을 실행한다는 것을 알 수 있다.
1 |
|
@ComponentScan
- @ComponentScan은 해당 어노테이션 하위에 있는 객체들 중 @Component가 선언된 클래스들을 찾아 Bean으로 등록하는 역할을 한다.
- 이 때 꼭 @Component가 아니여도 @Component가 선언되어 있는 어노테이션인 @Service, @Repository, @Controller 등등도 포함된다.
- @EnableAutoConfiguration이 스캔하기 전에 먼저 @ComponentScan이 진행된다.
1 |
|
참고 사이트
- https://www.youtube.com/watch?v=OXILjfY8edw&list=PLgXGHBqgT2TvpJ_p9L_yZKPifgdBOzdVH&index=7
- https://seongmun-hong.github.io/springboot/Spring-boot-EnableAutoConfiguration
- http://dveamer.github.io/backend/SpringBootAutoConfiguration.html
- https://www.baeldung.com/springbootconfiguration-annotation
- http://wonwoo.ml/index.php/post/20