Portfolio/Spring Boot

스프링 부트 4일차 - 스프링 부트 프로젝트 구조와 의존성 그리고 자동설정

Foo 2019. 4. 4. 23:26
728x90

@SpringBootApplication 가 붙은 메인 애플리케이션은 디폴트 패키지를 만든 후 그것에 @SpringBootApplication을 붙여주는 것이 좋음.

이유 : 컴포넌트 스캔을 하기 때문 해당 패키지 하위에 있는 Class 들을 Bean으로 등록하여 관리함.

         만약 디폴트 패키지가 아닌 src/main/java에 바로 Java 파일을 만들고 거기에 @SpringBootApplication을 붙이면? 내가 Bean으로 등록하고 싶지 않은 클래스들도 모두 Bean으로 등록된다.

  + 내생각 : Bean으로 관리할 필요 없는 클래스들도 Bean이 생성되면 그 만큼 리소스가 낭비될듯.

 참고 : https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-structuring-your-code

 

Spring Boot Reference Guide

This section dives into the details of Spring Boot. Here you can learn about the key features that you may want to use and customize. If you have not already done so, you might want to read the "Part II, “Getting Started”" and "Part III, “Using Spring Boot

docs.spring.io

 

 

스프링 부트로 생성된 내 프로젝트의 pom.xml은 spring-boot-starter-parent를 상속함.

만약 자세한 의존성 버전이 궁금하다면 spring-boot-dependencies-x.x.x.RELEASE.pom을 찾아서 버전을 확인하면 됨.

만약 해당 프로젝트에서 부모에서 상속받은 버전 말고 다른 버전을 사용하고 싶다면 내 프로젝트의 pom.xml에 버전을 명시해주면 됨. 그렇게하면 버전이 오버라이딩 됨.

spring-boot-starter-parent를 상속받지 않고 자신이 만든 의존성을 상속받을 수 있으나, 스프링 부트의 기본적인 설정을 사용하지 않기 때문에 권장하지 않음. (resource, utf-8 설정 등등)

 참고 : https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-dependency-management

 

Spring Boot Reference Guide

This section dives into the details of Spring Boot. Here you can learn about the key features that you may want to use and customize. If you have not already done so, you might want to read the "Part II, “Getting Started”" and "Part III, “Using Spring Boot

docs.spring.io

 

 

스프링 부트의 자동설정

@SpringBootApplication 은 다음 3가지를 합친것과 같음

@SpringBootConfiguration

@ComponentScan : Component들을 Bean으로 등록

@EnableAutoConfiguration : 스프링 메타 파일에 기록된 Bean들이 생성이 되어 등록되도록 함.

  • org.springframework.boot.autoconfigure.EnableAutoConfiguration