본문 바로가기
Web/Java, Spring(Boot)

[Spring] yaml 설정

by ju.__.nu 2025. 6. 26.

SpringBoot에 application.yaml은 Spring 프로젝트를 설정하기 위해 사용된다고 생각하면된다. (prot, database 연결, 인코딩...)

 

기존엔 xml을 많이 사용했는데 지금은 ,yaml을 더 많이 사용한다.

yaml이 xml보다 데이터가 덜 무겁기 때문이다.

xml 보다 경량 데이터 = json 보다 경량 데이터 = yaml

 

특징

  • 키 값에 쌍따옴표x (json엔 필요)
  • key와 value 사이엔 무조건 " "(스페이스 한칸)
  • 중괄호를 쓰지않고 들여쓰기로 구분, 들여쓰기는 "  "(스페이스 두칸)

스페이스 공간을 지키지 않으면 파일 실행x

 

기존  Spring 레거시와 비교

application.yaml = web.xml + root-context.xml + servlet-context.xml

  • root-context.xml : 한번만 생성하면 되는 데이터 관리 ex. DataBase
  • servlet-context : 지속적으로 생성해서 사용 할 데이터 관리

주로 이용하는 yaml 설명

서버 설정

프로젝트의 진입점을 설정 localhost:8000/blog를 진입점으로 하겠다는 설정

server:
  port: 8000
  servlet:
    context-path: /blog
    encoding:
      charset: UTF-8
      enabled: true
      force: true

해당 설정을 하지않으면 기본값 localhost:8080/ 이 기본값

 

스프링 설정

DB설정

데이터베이스 연결을 설정

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Seoul
    username: cos
    password: cos1234

mysql 드라이버에 cj가 들어간 이유는 mysql 6점대 이상 버전부터는 해당 드라이버를 사용하고 그 이전 드라이버는 cj가 없는 com.mysql.jdbc.Driver를 사용한다.

 

JSP 설정

Spring은 기본적으로 .jsp를 지원x

→ pom.xml에 jsp 템프릿 엔진 의존성 설정 추가

spring:
  mvc:
    view:
      prefix: /WEB-INF/views/
      suffix: .jsp

해당 설정을 하지않으면 @Controller의 return 값에 기본경로를 붙이는데 .jsp를 인식x

→ 파일리턴 기본경로: src/main/resources/static

→ 정적인 파일만 인식하기 때문

그래서 경로를 변경해줌

 

// 예시 코드
@GetMapping("temp/jsp")
public String tempJsp() {
    System.out.println("tempjsp");
    // 설정한 리턴 경로
    // prefix: /WEB-INF/views/
    // suffix: .jsp
    // 리턴명 앞엔  "/WEB-INF/views/" 뒤엔 ".jsp" 붙음
    // 그래서 "test"를 리턴하면
    // "/WEB-INF/views/test.jsp" 이 리턴
    return "test";
}

 

jpa 설정

spring:
  jpa:
    open-in-view: true
    hibernate:
      ddl-auto: create
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    show-sql: true
    properties:
      hibernate.format_sql: true

ddl-auto: create

→ 프로젝트를 실행할 때 마다 테이블을 새로만듬 (create/update/none)

 

pysical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

→ 변수명을 그래도 컬럼명으로 지정하는 전략

→ org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy 를 사용하면 변수명에 언더코어를 붙여줌

 

show-sql: true

→ console에 sql문을 출력 (한줄로 출력)

hibernate.format.sql: true

→ sql문을 이쁘게 정렬시켜줌


참고하면 좋을 자료

https://www.inflearn.com/community/questions/16184/yaml%ED%8C%8C%EC%9D%BC-%EC%9D%B4%EB%9E%80-%EB%AC%B4%EC%97%87%EC%9D%B8%EA%B0%80%EC%9A%94

 

yaml파일 이란 무엇인가요 - 인프런 | 커뮤니티 질문&답변

누구나 함께하는 인프런 커뮤니티. 모르면 묻고, 해답을 찾아보세요.

www.inflearn.com

 

 

 

 

'Web > Java, Spring(Boot)' 카테고리의 다른 글

[Spring] Spring 특징  (0) 2025.07.08
[Spring] 패키지명을 통일해야하는 이유  (1) 2025.06.04