본문 바로가기
코딩 공부/web & Java

[Spring] SpringBoot Auto Configuration

by 현장 2023. 10. 12.

SpringBoot Auto Configuration

@SpringBootApplication
public class LearnSpringBootApplication {

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

}

일반적으로 Spring Boot를 사용하여 웹 애플리케이션을 빌드할 때면 많은 설정이 필요합니다. 컴포넌트 스캔,  DispatcherServlet을 설정해야 하고 데이터베이스와 통신하려면 데이터 소스를 설정 등 많은 것들을 설정해야 합니다. 이와 같은 작업을 간소화하기 위해 환경 설정을 자동으로 해주는 SpringBoot Auto Configuration를 사용합니다.

 

자동 설정은 클래스 경로에 있는 프레임워크 따라 생성되며 pom.xml여러 설정을 추가하여 프레임워크를 가져오게 됩니다. 따라서 클래스 경로에 있는 프레임워크에 따라 많은 것을 자동 설정할 수 있습니다. 그리고  Spring Boot의 디폴트 설정을 제공하며 이를 오버라이드 할 수도 있습니다. 

이 두 가지가 모두 Auto Configuration을 결정하는 데 사용되며 클래스 경로에 있는 프레임워크나 클래스, 제공된 기존 설정에 따라 결정됩니다.

 

또한 모든 Auto Configuration 로직은 특정 jar에서 정의되는데,  spring-boot-autoconfigure.jar에서 확인할 수 있습니다.

 

자동 설정을 좀 더 자세히 알아보려면 application.properties에서 로깅 수준을 설정할 수 있습니다, 이를 열어

logging.level.org.springframework=debug

라고 입력합니다. 그후 실행해 보시면

============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   AopAutoConfiguration.ClassProxyingConfiguration matched:
      - @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)
// 생략
Negative matches:
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'jakarta.jms.ConnectionFactory' (OnClassCondition)

   AopAutoConfiguration.AspectJAutoProxyingConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)
// 생략

여러 가지가 추가로 나오게 되는데 위와 같이 로그를 자세히 살펴보면 CONDITIONS EVALUATION REPORT가 나오게 됩니다.

여기에는 Positive matches와 Negative matches가 있는데 Negative matches는 자동 설정되지 않은 항목이고 Positive matches는 자동 설정된 항목을 나타냅니다.

로깅 수준
로깅 수준은 trace, debug, info, warning, error, off 순으로 되어 있으며 예를 들어 info로 설정을 하면
info, warning, error, off의 정보를 모두 출력하게 됩니다.