코드 디버깅은 애플리케이션 내 동작을 구축, 식별, 분리하는 한 단계에 불과하다.
동적이고 분산된 애플리케이션이 많아지면, 다음의 단계를 실행해야 한다.
지금부터 스프링 부트에 내장된 설정을 유연하게 해 주는 기능들을 살펴볼꺼다.
개발 환경에서 앱 개발을 더욱 편리하게 해 준다.
주요 기능
Spring Initializr에서 Add dependencies…를 통해 추가하거나,
pom.xml 파일에 DevTools 의존성을 추가할 수 있다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
외부 설정 파일(application.properties 또는 application.yml)의 값을 Java 클래스에 매핑해 준다.
예제를 보자면,
application.properties
app.name=MyApp
app.description=This is a sample application
설정 클래스
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private String description;
// Getter와 Setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
사용 예시
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AppController {
private final AppProperties appProperties;
@Autowired
public AppController(AppProperties appProperties) {
this.appProperties = appProperties;
}
@GetMapping("/app-info")
public String getAppInfo() {
return "Name: " + appProperties.getName() + ", Description: " + appProperties.getDescription();
}
}
특정 설정 값을 개별적으로 주입받을 때 사용
설정 파일 application.properties
app.name=MyApp
주입 코드
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppNamePrinter {
@Value("${app.name}")
private String appName;
public void printAppName() {
System.out.println("Application Name: " + appName);
}
}
추가적인 프로퍼티 파일을 로드할 때 사용
설정 파일 application.properties
app.version=1.0.0
설정 로드 클래스
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:additional.properties")
public class AdditionalConfig {
// 추가 설정 파일에서 값을 로드
}
프로파일을 통해 환경별 설정을 쉽게 관리할 수 있다.
application-dev.properties
app.environment=development
application-prod.properties
app.environment=production
프로파일 활성화 application.properties
spring.profiles.active=dev
설정 주입
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EnvironmentPrinter {
@Value("${app.environment}")
private String environment;
public void printEnvironment() {
System.out.println("Current Environment: " + environment);
}
}
application.properties 파일 외에도, application.yml을 사용할 수 있다.
application.yml
app:
name: MyApp
description: This is a sample application
설정 클래스
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private String description;
// Getter와 Setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
테스트 환경에서 별도의 설정 파일을 사용할 수 있도록 지원
test.properties
app.name=TestApp
테스트 클래스
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
@SpringBootTest
@TestPropertySource(locations = "classpath:test.properties")
public class AppPropertiesTest {
@Autowired
private AppProperties appProperties;
@Test
public void testAppName() {
assertEquals("TestApp", appProperties.getName());
}
}
데이터베이스, 메시징 시스템, 캐시 등의 서드 파티 라이브러리와 쉽게 통합할 수 있다.
Spring Data JPA와 통합 예제.
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
애플리케이션 실행 시 자동으로 설정된 빈과 프로퍼티들을 리포트하는 기능
pom.xml에 의존성 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.properties 파일에서 Actuator 엔드포인트를 활성화
management.endpoints.web.exposure.include=*
애플리케이션의 모니터링과 관리를 위한 다양한 기능을 제공
주요 엔드포인트
pom.xml 파일에 Actuator 의존성을 추가
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.properties 파일에서 Actuator 엔드포인트를 활성화
management.endpoints.web.exposure.include=*
애플리케이션을 실행하고, 브라우저에서 다양한 Actuator 엔드포인트에 접근하여 정보를 확인할 수 있다.
ex) http://localhost:8080/actuator/health
다양한 기능들을 통해 스프링 부트는 애플리케이션 설정을 유연하게 관리하고, 개발 및 운영 환경에서 효과적으로 애플리케이션을 관리할 수 있다.
아래 링크에서 더 많은 내용을 볼 수 있다.
https://docs.spring.io/spring-boot/index.html
이 본문은 책 spring boot up & running 처음부터 제대로 배우는 스프링 부트 책의 내용을 담고 있다.