application.properties, YAML

김승규·2024년 3월 4일

Externalized Configuration

  • spring-boot는 같은 소스코드로 여러 환경에서 동작할 수 있도록 외부화 설정을 제공한다.
  • java properties, YAML, 환경변수, 실행 인자로 설정 가능
  • 전체 프로젝트의 설정은 .properties, .yaml 중 하나만 사용하는 것을 권장
  • 같은 곳에 application.propreties, application.yaml 이 동시에 존재하면 application.propreties 가 우선함

jpa

# JPA table create and SQL logging.
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

datasource

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://{host}:{port}/{database}?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8

# entity - db name mapping strategy
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.datasource.username={username}
spring.datasource.password={password}
  • org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl : 엔티티의 이름 그대로 매핑
  • org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy : 기본값이다. 엔티티의 CamelCase 네이밍 규칙을 언더바 네이밍 규칙으로 매핑

thymeleaf

  • view template 의 경로변경
  • 만약, application.properties 에 아래와 같이 설정되어 있고 getStudent 메서드가 "student"를 반환한다면 템플릿은 "/src/main/resources/templates/main/student.html" 에 위치해야 합니다.
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/main/
spring.thymeleaf.suffix=.html

서버 설정

  • spring-boot가 제공하는 @ConfigurationProperties 바인딩으로 동작
  • spring-boot-autocofiguration.jar:org.springframework.boot.ServerProperties 에서 @ConfigurationProperties 바인딩 제공
server.port=8888
server.address=
server.error.pathh-
server.error.includeException=

YAML (application.yaml)

nhn:
  student:
    name: zbum
profile
꿈꾸는 리얼리스트 개발자 김승규입니다.

0개의 댓글