profile๋ก ํ๊ฒฝ๋ณ ์ค์ ์ ์ฉ
yml ํ์ผ์ profile๋ก local, dev ํ๊ฒฝ๋ณ ์ค์ ์ ๋ฐ๋ก ์ ์ฉํด๋ณด์.
applicatio.yml
#...
server:
port: 5000
servlet:
context-path: /
encoding:
charset: UTF-8
enabled: true
force: true
spring:
profiles: # profiles ์ค์
active: local # ๋ค๋ฅธ ์ค์ ์ด ์์ ๋ default ํ๊ฒฝ ๊ฐ
devtools:
livereload:
enabled: true
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true
#...
# application.yml์ ๊ณตํต์ผ๋ก ์ ์ฉํ ์ค์ ์ ์ ๋๋ค.
spirng.profiles.active์๋ ๋ฐ๋ก ์ง์ ํด๋ ์ค์ ์ด ์์ ๋ default ๊ฐ์ ์ ์ด๋๋ค.
์์ ์์ค์์ ๋ค๋ฅธ ์ค์ ์ด ์์ ์ local๋ก ํ๋ก์ ํธ๊ฐ ๋น๋ ๋๋ค.
application-dev.yml, applicatio-local.yml

ํ๋ก์ ํธ์ src/main/resources์ ํ๋จ์ ์๋ application.yml๊ณผ ๋์ผํ ์์น์ ์ค์ ํ๊ณ ์ถ์ ํ๊ฒฝ ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ๋ง๋ค์ด์ค๋ค.application-{ํ๊ฒฝ๋ช
}.yml
application-dev.yml
spring:
jpa:
hibernate:
ddl-auto: update #create update none
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: {db url}
username: {db username}
password: {db password}
mail:
host: smtp.gmail.com
port: 587
username: {smtp mail service username}
password: {smtp mail service password}
properties:
mail:
smtp:
auth: true
starttls:
enable: true
local๊ณผ ๋ค๋ฅด๊ฒ ํ ์ค์ ๋ค์ application-dev.yml
์ ์ธํ
ํ๋ค.
ddl-auto ๋ฐฉ์์ด๋ local ๊ณผ๋ ๋ค๋ฅธ db ์ค์ , ๊ทธ๋ฆฌ๊ณ ๋ฐฐํฌ ์์๋ง ์ฌ์ฉํ mail ๊ธฐ๋ฅ์ ์ถ๊ฐํ์๋ค.
application-local.yml
spring:
jpa:
hibernate:
ddl-auto: create #create update none
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: {db url}
username: {db username}
password: {db password}
local ๊ฐ์ ๊ฒฝ์ฐ์ ๋ฐฐํฌํ ๋์ ๋ค๋ฅธ db๋ฅผ ์ฌ์ฉํ๊ณ ddl-auto ๋ฐฉ์๋ create, mail ๊ธฐ๋ฅ์ ์ ์ฐ๊ธฐ ๋๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ์๋ค.
ConsoleMailSender.java
@Profile("local")
@Slf4j
@Component
public class ConsoleMailSender implements JavaMailSender {
// ...
}
๋ก์ปฌ์์๋ ์ง์ ๋ฉ์ผ์ ์ ์กํ์ง ์์ผ๋ฏ๋ก ์์ ๊ฐ์ด JavaMailSender
์ implements ํ ํด๋์ค๋ก ๋ก๊น
์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์๋ค.
์ด ๋ ํด๋น ํด๋์ค๋ local์์๋ง ์ฐ์ด๋ฏ๋ก @Profile("local")
์ด๋
ธํ
์ด์
์ ์ถ๊ฐํด์ฃผ์๋ค.
๋น๋
IntelliJ

IntelliJ IDE๋ฅผ ์ด์ฉํ ์์ ๋ค์๊ณผ ๊ฐ์ด ์ฐ์ธก์๋จ์์ Edit Configurations
๋ฅผ ํด๋ฆญํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ฐฝ์ด ๋์จ๋ค.

์ค๊ฐ์ ์๋ Active Profiles์ ์ํ๋ ํ๊ฒฝ์ ์
๋ ฅํ๋ฉด ๋๋ค.
๊ณต๋์ผ๋ก ๋ ์์๋ application.yml์ ์๋ default ๊ฐ์ผ๋ก ์ค์ ๋๋ค.
์๋ฒ๋ก ๋ฐฐํฌ ์
์๋ฒ์์๋ -Dspring.profiles.active
์ต์
์ ์ ์ฉํ๋ฉด ๋๋ค.
$ java -jar -Dspring.profiles.active={์ง์ ํ ํ๊ฒฝ} api-0.0.1-SNAPSHOT.jar
์์ ์ง์ ํ ํ๊ฒฝ
์ ์ํ๋ ํ๊ฒฝ์ ์
๋ ฅํ๋ฉด ํด๋น ํ๊ฒฝ์ผ๋ก ๋ฐฐํฌ๋๋ค.
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring์ DI, IoC ์ปจํ ์ด๋, ์คํ๋ง ๊ณ์ธต๊ตฌ์กฐ (0) | 2022.03.16 |
---|---|
@ControllerAdvice๋ฅผ ์ด์ฉํ ์์ธ ์ฒ๋ฆฌ (0) | 2022.03.16 |
Spring ํ๋ก์ ํธ์ Swagger2 ์ ์ฉ (0) | 2022.03.15 |
Validation์ผ๋ก ์์ฒญ ๊ฐ ๊ฒ์ฆ (0) | 2022.03.15 |
์๋ฒ ์คํ ์ DB Table ์์ฑ (0) | 2022.03.15 |