Spring/Tistory

블로그-V3. 3가지 설정 파일

JJJAEOoni 2022. 6. 8. 23:17
반응형

 

 

스프링은 설정 파일의 application- 뒤에 붙는 이름을

설정 파일의 이름으로 본다.

규칙!

 

이 프로파일명으로 어떤 설정 파일로 프로그램을 실행시킬지 결정한다.

 

spring:
  profiles:
    active:
    - dev

 

기본적으로 잡은 프로파일이 dev이기 때문에 특별한 설정을 해주지 않으면 dev 파일로 실행된다.

 

직접 프로그램을 실행해서 포스트맨을 사용해 테스트할 때는 dev로 실행하고,

JUnit으로 테스트할 때는 test 파일로 실행하고 싶다.

 

그래야 내 DB에 영향을 미치지 않을 것 같은데?

 

해당 파일을 어떤 설정 파일로 실행할 지 선택할 수 있는 어노테이션 @ActiveProfiles

@ActiveProfiles("test") // test 설정파일로 실행 -> h2
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class UserControllerTest {

}

 

 

application-dev.yml

# 개발 할 때 dev
server:
  port: 8080
  servlet:
    context-path: /
    encoding:
      charset: utf-8
    session:
      timeout: 30

spring:
  mail:
    host: smtp.gmail.com # smtp 프로토콜
    port: 587
    username: 아이디
    password: 비밀번호
    properties:
      mail:
        smtp:
          starttls:
            enable: true
            required: true
          auth: true
  security:
    user:
      name: test
      password: 1234
  mustache:
    expose-session-attributes: true
  datasource:
      url: jdbc:mariadb://localhost:3306/greendb
      driver-class-name: org.mariadb.jdbc.Driver
      username: green
      password: green1234


  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

  output:
    ansi:
      enabled: always

file:
  path: C:/Users/green/upload/ 

logging:
  level:
    '[org.springframework.web]': DEBUG
    '[org.hibernate]': DEBUG

 

application-prod.yml

# 배포 할 때 prod
server:
  port: 8080
  servlet:
    context-path: /
    encoding:
      charset: utf-8
    session:
      timeout: 30

spring:
  mail:
    host: smtp.gmail.com
    port: 587
    username: 아이디
    password: 비밀번호
    properties:
      mail:
        smtp:
          starttls:
            enable: true
            required: true
          auth: true
  mustache:
    expose-session-attributes: true
  datasource:
      url: jdbc:mariadb://아이피:3306/greendb
      driver-class-name: org.mariadb.jdbc.Driver
      username: 유저명
      password: 비밀번호


  jpa:
    open-in-view: true
    hibernate:
      ddl-auto: none # 주의!
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

file:
  path: /home/ubuntu/upload/ 

logging:
  level:
    '[org.springframework.web]': INFO
    '[org.hibernate]': INFO

 

application-test.yml

# 테스트 할 때 test
server:
  port: 8080
  servlet:
    context-path: /
    encoding:
      charset: utf-8
    session:
      timeout: 30

spring:
  mail:
    host: smtp.gmail.com # smtp 프로토콜
    port: 587
    username: 아이디
    password: 비밀번호
    properties:
      mail:
        smtp:
          starttls:
            enable: true
            required: true
          auth: true
  security:
    user:
      name: test
      password: 1234
  mustache:
    expose-session-attributes: true
  datasource:
      url: jdbc:h2:mem:test
      driver-class-name: org.h2.Driver
      username: sa
      password: 


  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

  output:
    ansi:
      enabled: always

file:
  path: C:/Users/green/upload/ 

logging:
  level:
    '[org.springframework.web]': DEBUG
    '[org.hibernate]': DEBUG

 

 

 

[출처]

 

https://cafe.naver.com/metacoding

 

메타코딩 : 네이버 카페

코린이들의 궁금증

cafe.naver.com

메타 코딩 유튜브

https://www.youtube.com/c/%EB%A9%94%ED%83%80%EC%BD%94%EB%94%A9

 

메타코딩

문의사항 : getinthere@naver.com 인스타그램 : https://www.instagram.com/meta4pm 깃헙 : https://github.com/codingspecialist 유료강좌 : https://www.easyupclass.com

www.youtube.com

 

 

반응형