spring.profiles.active를 이용한 분기처리

Sunny·2022년 7월 22일
0

업무를 하다보면 맡고 있는 홈페이지가 운영인지 개빌인지에 따라 분기처리를 해야하는 경우가 있다.
만약, SpringBoot를 사용한다면 다음 방법을 이용하여 분기처리가 가능하다!

🌱 spring.profiles.active를 이용한 분기처리 (Java)

다음 예시처럼 사용하면 된다.

String profile = System.getProperty("spring.profiles.active");

if(profile == "dev" || profile == "local"){
	server_ip = "192.xxx.xx.xx";
}
else if(profile == "real"){
	server_ip = "192.xxx.xx.xx";
}

🌱 spring.profiles.active를 이용한 분기처리 (jsp)

  1. 다음 태그를 추가한다.
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
  1. spring:eval 태그를 추가한다.
<spring:eval expression="@enviroment.getProperty('spring.profiles.active')" var="activeProfile" />
  1. c:choose를 이용하여 분기처리한다.
<c:choose>
  <c:when test="${activeProfile eq 'local'}">
       <!-- 하고 싶은 분기처리 코드 작성 -->
  </c:when>  
  <c:when test="${activeProfile eq 'dev'}">
       <!-- 하고 싶은 분기처리 코드 작성 -->
  </c:when> 
  <c:when test="${activeProfile eq 'real'}">
       <!-- 하고 싶은 분기처리 코드 작성 -->
  </c:when> 
</c:choose>

▶ 위의 코드에서 local, dev, real은 application.yml 파일에서 설정한 것이다.
▶ 각자 설정한대로 쓰면 된다!

profile
개발에 재미를 붙여보기 :)

1개의 댓글

comment-user-thumbnail
2022년 10월 27일

enviroment 에서 n 빠져있어요......

답글 달기