-
자동설정 만들기개발/Spring Boot 2023. 7. 21. 11:23
자동 설정 만들기 1부: Starter와 Autoconfigure
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-developing-auto-configurationSpring Boot Reference Documentation
This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe
docs.spring.io
● Xxx-Spring-Boot-Autoconfigure 모듈: 자동 설정
● Xxx-Spring-Boot-Starter 모듈: 필요한 의존성 정의
● 그냥 하나로 만들고 싶을 때는?
○ Xxx-Spring-Boot-Starter● 구현 방법
1. 의존성 추가
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>2. @Configuration 파일 작성
3. src/main/resource/META-INF에 spring.factories 파일 만들기
4. spring.factories 안에 자동 설정 파일 추가
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
FQCN,\
FQCN
5. mvn install해당 만든 모듈을 부모프로젝트에서 dependency하면 모듈안 bean을 바로 사용가능.
다만, 해당 bean을 부모프로젝트에서 bean으로 재정의 해도 값이 변경되지 않는다.
이유는 bean 등록 순서가 componentScan이 먼저고 나중에 autoconfigure가 되기 때문이다.
반대로 부모프로젝트에서의 bean을 우선으로 불러오려면
@ConfigurationProperties
● 덮어쓰기 방지하기
○ @ConditionalOnMissingBean -> 부모프로젝트에서 모듈에서 쓰는 bean이 이미 있으면 만들지 않는다.
다른방법 : bean으로 만들기 싫다. application.properties에서 정의 후 아래 @ConfigurationProperties로 불러온다.
● 빈 재정의 수고 덜기
○ @ConfigurationProperties(“holoman”)
○ @EnableConfigurationProperties(HolomanProperties) ○ 프로퍼티 키값 자동 완성
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>'개발 > Spring Boot' 카테고리의 다른 글
내장 웹 서버 응용 (0) 2023.07.21 내장 웹 서버 이해 (0) 2023.07.21 스프링 부트 원리 (0) 2023.07.20 스프링 부트 구조 (0) 2023.07.20 스프링 부트 시작하기 (0) 2023.07.20