본문 바로가기
Programming/Spring Boot

프로파일(Profile)

by yoon9i 2024. 7. 2.

11. 프로파일(Profile)
https://docs.spring.io/spring-boot/docs/2.7.18/reference/htmlsingle/#features.external-config.files.profile-specific

1> 개요
- 실제로 개발할 때는 개발환경, Q/A 환경, .. ,Production 환경
  다양한 환경을 개발자가 선택적으로 정해서 환경을 맞출 수 있는 개념.

ex>
  # 개발환경
  Application.java ----------> DeptServiceImpl ------------> DeptDAO -------------> H2(데이터베이스, 인메모리)

  # Production환경(배포)
  Application.java ----------> DeptServiceImpl ------------> DeptDAO -------------> MySQL(데이터베이스)


2> profile 에 따라서 properties 파일 선택 방법
여러개의 application.properties 필요

문법:
    application-포르파일명.properties|yml

- application.properties (기본, 필수)
  # 동작할 프로파일명 지정
  spring.profiles.active = 프로파일명
  ex>
      // spring.profiles.active = dev
      spring.profiles.active = prod <== 지정한 프로파일명에 해당하는 application-프로파일.properties
                                        가 실제로 실행됨.

  application-dev.properties (개발용)
  application-prod.properties (product 용)


3> profile 에 따라서 빈(Bean) 파일 선택 방법

    application.properties (기본)
    # 동작할 프로파일명 지정
    spring.profiles.active=prod
    
    @Profile("dev")
    public class DevBean() {}

    @Profile("prod")
    public class DevBean() {}

 

package com.exam;

import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import com.exam.service.DevBean;
import com.exam.service.ProdBean;

@SpringBootApplication
public class Application implements CommandLineRunner{

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
	
	//IoC Container 참조
    Logger logger = LoggerFactory.getLogger(getClass());
	
    // IoC Container
    @Autowired // 생성자주입 대신에 사용가능.
	ApplicationContext ctx;
	// 생성자 주입
	public Application(ApplicationContext ctx) {
		logger.info("logger:ApplicationContext:{}","Application 생성");
		this.ctx = ctx;
	}

	@Override
	public void run(String... args) throws Exception {
		logger.info("logger:ApplicationContext:{}",ctx);
		
		logger.trace("trace: {}, {}", "trace1", "trace2");
		logger.debug("debug: {}", "debug");
		logger.info("info: {}", "info");
		logger.warn("warn: {}", "warn");
		logger.error("error: {}", "error");
		
	}

}
package com.exam.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

//@Service 사용해도 괜챃음.
@Component
@Profile("dev")
public class DevBean {
	Logger logger = LoggerFactory.getLogger(getClass());
	
	public DevBean() {
		logger.info("logger:{}", "DevBean 생성");
	}
}
package com.exam.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

//@Service 사용해도 괜챃음.
@Component
@Profile("prod")
public class ProdBean {
	Logger logger = LoggerFactory.getLogger(getClass());
	
	public ProdBean() {
		logger.info("logger:{}", "ProdBean 생성");
	}
}
# application.properties
logging.level.com.exam=error

# profile 선택
spring.profiles.active=prod
# application-dev.properties
logging.level.com.exam=debug

# h2 설정정보 저장
# application-prod.properties
logging.level.com.exam=info

# mysql 설정정보저장
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.18</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.exam</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>