Spring Framework에서 지원하는 Profile 기능은 애플리케이션의 설정을 환경(예: 운영, 개발, 테스트 등)에 따라 분리하고 관리할 수 있게 해줍니다.
Profile을 사용하면 같은 애플리케이션에서도 다른 환경에서 다르게 동작하도록 설정을 변경하는 것이 간단해집니다.
@Profile
어노테이션을 사용하여 코드 수준에서 특정 프로파일에서만 활성화될 빈(Bean)을 정의할 수 있습니다.저는 application.yaml
을 다음과 같이 정의하였습니다.
spring:
profiles:
group:
"prod": "prod, common"
"dev": "dev, common"
---
spring:
config:
activate:
on-profile: prod
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimeZone=Asia/Seoul
username: root
password:
server:
port: 8081
---
spring:
config:
activate:
on-profile: dev
datasource:
url: jdbc:h2:mem:test
driver-class-name: org.h2.Driver
username: sa
password:
h2:
console:
enabled: true
path: /h2-console
server:
port: 8082
---
spring:
config:
activate:
on-profile: "common"
sql:
init:
mode: always
즉, profile이 prod일 때에는 -> prod, common 이 실행되고
profile이 dev일 때에는 -> dev, common이 실행됩니다.
prod 환경에서는 server port를 8081을 이용하며, db로는 mysql을 사용히며
dev 환경에서는 server port를 8082를 사용하며, db로는 h2를 사용합니다.
그럼 jar 파일로 서버를 띄울 때에 --spring.profiles.active
옵션을 이용해 Profile을 지정해서 각각을 띄워보도록 하겠습니다.
demo-0.0.1-SNAPSHOT.jar
➜ libs java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.16)
2023-10-15 17:37:47.690 INFO 36893 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 11.0.11 on isujin-ui-MacBookAir.local with PID 36893 (/Users/isujin/study/java/demo/build/libs/demo-0.0.1-SNAPSHOT.jar started by isujin in /Users/isujin/study/java/demo/build/libs)
2023-10-15 17:37:47.710 INFO 36893 --- [ main] com.example.demo.DemoApplication : The following 2 profiles are active: "prod", "common"
2023-10-15 17:37:49.204 INFO 36893 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-10-15 17:37:49.253 INFO 36893 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 32 ms. Found 0 JPA repository interfaces.
2023-10-15 17:37:50.890 INFO 36893 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2023-10-15 17:37:50.931 INFO 36893 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-10-15 17:37:50.932 INFO 36893 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.80]
2023-10-15 17:37:51.089 INFO 36893 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-10-15 17:37:51.089 INFO 36893 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3256 ms
2023-10-15 17:37:51.535 INFO 36893 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-10-15 17:37:51.945 INFO 36893 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-10-15 17:37:52.132 INFO 36893 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-10-15 17:37:52.229 INFO 36893 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.15.Final
2023-10-15 17:37:52.555 INFO 36893 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-10-15 17:37:52.714 INFO 36893 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
2023-10-15 17:37:52.983 INFO 36893 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-10-15 17:37:53.001 INFO 36893 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-10-15 17:37:53.145 WARN 36893 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-10-15 17:37:54.065 INFO 36893 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2023-10-15 17:37:54.088 INFO 36893 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 7.637 seconds (JVM running for 8.491)
다음과 같이 두 개의 profile인 "prod", "common"이 정상적으로 active 되었고, 포트 8081을 이용하고, mysql에 정상적으로 연결됨을 확인할 수 있습니다.
이어서 다음 Profile도 적용해서 실행해보면 다음과 같습니다.
➜ libs java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.16)
2023-10-15 17:50:57.004 INFO 37303 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 11.0.11 on isujin-ui-MacBookAir.local with PID 37303 (/Users/isujin/study/java/demo/build/libs/demo-0.0.1-SNAPSHOT.jar started by isujin in /Users/isujin/study/java/demo/build/libs)
2023-10-15 17:50:57.067 INFO 37303 --- [ main] com.example.demo.DemoApplication : The following 2 profiles are active: "dev", "common"
2023-10-15 17:50:58.539 INFO 37303 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-10-15 17:50:58.588 INFO 37303 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 30 ms. Found 0 JPA repository interfaces.
2023-10-15 17:51:00.171 INFO 37303 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8082 (http)
2023-10-15 17:51:00.205 INFO 37303 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-10-15 17:51:00.205 INFO 37303 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.80]
2023-10-15 17:51:00.367 INFO 37303 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-10-15 17:51:00.368 INFO 37303 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3179 ms
2023-10-15 17:51:00.474 INFO 37303 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-10-15 17:51:00.720 INFO 37303 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-10-15 17:51:00.747 INFO 37303 --- [ main] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:test'
2023-10-15 17:51:01.331 INFO 37303 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-10-15 17:51:01.420 INFO 37303 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.15.Final
2023-10-15 17:51:01.764 INFO 37303 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-10-15 17:51:01.941 INFO 37303 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2023-10-15 17:51:02.282 INFO 37303 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-10-15 17:51:02.297 INFO 37303 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-10-15 17:51:02.464 WARN 37303 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-10-15 17:51:03.318 INFO 37303 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''
2023-10-15 17:51:03.347 INFO 37303 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 7.615 seconds (JVM running for 8.714)
다음과 같이 두 개의 profile인 "dev", "common"이 정상적으로 active 되었고, 포트 8082을 이용하고, h2 database에 정상적으로 연결됨을 확인할 수 있습니다.