Spring은 사용하려는 프로파일을 정의하여 상황에 따라 컴포넌트에 대한 등록 및 제외를 결정할 수 있다.

@Profile({"dev", "!kr"})

이라고 프로파일을 정의하면 조건식은 dev or !kr 이 되어 dev 혹은 kr 에 대해서 선언되어 있지 않으면 반드시 실행되는 상황이 발생한다.

이런 상황을 피할 수 있는 방법으로 Spring 4.0에서 추가된 @@Conditional 을 사용하는 방법이 있다. 이와 관련한 질문은 How to conditionally declare Bean when multiple profiles are not active? 를 살펴보면 고민하고 있는 유사한 내용과 답변을 볼 수 있다.

간단한 해결책은 Condition을 구현하는 것이다. 다음과 같이 간단하게 dev and !kr을 만족하는 조건식을 작성해보자.

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class PrdAndIgnoreKrProfileCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getEnvironment().acceptsProfiles("prd") && !context.getEnvironment().acceptsProfiles("kr");
}
}

위의 클래스를 사용하여

@Profile({"dev", "!kr"})
//을 대신하여
@Conditional(PrdAndIgnoreKrProfileCondition.class)
// 으로 정의하면 dev and !kr 조건식이 적용가능해진다.


현재 회사에서는 Spring Boot + AWS Beanstalk 조합으로 서비스를 운영하고 있다. 빈즈톡을 이용하여 로드밸런싱 처리를 할 때 nginx 를 사용하고자 하는 경우 빈즈톡에서 사용하는 인스턴스 내에서 nginx 설정을 변경해도 반영되지 않는 문제가 발생한다.

AWS 의 개발가이드가 완전히 한국어화가 되지 않았기 때문에 찾아보기도 이해하기도 쉽지 않다.

이에 프로젝트 내에 빈즈톡과 관련된 설정파일을 두어 배포시 빈즈톡 내에 위치한 nginx 설정 파일 /etc/nginx/conf.d 에 위치하도록 할 수 있다.

적용방법

  1. 프로젝트 내에 aws 설정파일(.conf) 를 둘 디렉토리를 지정하라.

    • ex) $ mkdir boot-spring-boot/aws

  2. boot-spring-boot/aws 하위에 .ebextensions/nginx/conf.d/elasticbeanstalk 디렉토리를 생성한다.

엘라스틱 빈즈톡의 기본 nginx 를 확장하기 위해서는 .ebextensions/nginx/conf.d/ 폴더에 .conf 설정파일을 추가하면 된다. 그러면 자동으로 엘라스틱 빈즈톡의 nginx 구성에 .ebextensions/nginx/conf.d/ 에 있는 .conf 파일들이 추가된다.

~/workspace/boot-spring-boot/aws/
`-- .ebextensions
    `-- nginx
        `-- conf.d
            |-- elasticbeanstalk
            |   `-- 00_server.conf
            `-- boot-spring-boot.conf

conf.d 폴더에 있는 .conf 파일들은 기본 구성의 http 블럭에 포함되며, conf.d/elasticbeanstalk 폴더에 있는 파일의 내용은 http 블록 내에 server 블록에 포함된다.

엘라스틱 빈즈톡의 기본 nginx 구성을 완전히 덮어쓰기 위해서는 .ebextensions/nginx/nginx.conf 으로 구성을 추가하면 된다.

~/workspace/boot-spring-boot/aws/
`-- .ebextensions
    `-- nginx
        `-- nginx.conf

nginx 에서 파일업로드 사이즈를 10M로 올려보자.

  1. 프로젝트 내에 aws/.ebextensions/nginx/conf.d/elasticbeanstalk 폴더를 생성

  2. 00_server.conf 파일 생성

  3. 00_server.conf 내에 client_max_body_size 10M; 를 추가한다.

  4. 스프링 부트 패키징 시 .ebextensions/nginx/conf.d/elasticbeanstalk 폴더가 포함되어 배포되도록 만든다.

build.gradle 에 다음과 같은 태스크를 만들어 실행하면 손쉽게 가능해진다.

task awsEBZip(type: Zip, dependsOn: 'bootRepackage') {
from '../aws' // .ebextensions 위치
from 'build/libs/' + jar.archiveName bootRepackage 된 파일 위치
baseName = 'boot-spring-boot-' + jar.version // 재생성한 파일명
}



Thymeleaf 에서 스프링 환경변수 사용하기

Thymeleaf 에서 스프링 프로퍼티 값 사용하는 방법

  • @ 뒤에 빈(Bean) 이름을 사용하면 그 빈에 접근할 수 있다.

타임리프에서 스프링의 property 파일(application.property 혹은 application.yml 등)에 기술되어 있는 변수를 이용하려는 경우

${@environment.getProperty('property.key')}

프로파일 환경에 따라 표시를 하려면

<div th : if = $ { @environment .acceptsProfiles ( 'production' )}>
This is the production profile
</ div>
or
<div th : if = "$ {# arrays.contains (@ environment.getActiveProfiles () 'production')} " >
     This is the production profile
</ div>

시스템 환경변수를 이용하는 경우

$ { @systemProperties [ 'property.key' ]}