반응형

properties를 아래와 같이 개발/운영 분리해서 사용할 경우 동적으로 properties를 셋팅할 수 있는 방법을 설명하려고 한다. 

- properties를 분리해야 하는 이유

  개발 진행 시 DB연결 정보나 외부 API정보들을 로컬/개발/운영 등 환경에 따라

  맞는 정보를 불러와야 함(properties하나로 사용할 경우 데이터 및 주석 변경이 번거롭게 필요)

  내용 수정 후 다시 컴파일 해야함

  유지보수, 보안성 측면에서도 고정된 설정정보보다 분리하여 관리하는것이 좋음

 

1. context-common.xml 설정 

context-common.xml 상단 beans에 스키마를 추가해야 함

util:properties 사용하기  

 - xmlns:util="http://www.springframework.org/schema/util" 추가

 - xsi:schemaLocation내에 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd 추가

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:beans="http://www.springframework.org/schema/cache"
   xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<context:annotation-config/>

<!-- dynamic config.properties -->
<util:properties id="config" location="classpath:properties/#{systemProperties['spring.profiles.active']}/db.properties"/>
</beans>

<util:properties id="프로퍼티ID" location="classpath:경로/프로퍼티명"/> 선언

경로를 위와같이 설정하면 systemProperties를 불러와야하므로

로컬 톰캣과 같은 경우는 아래와 같이 설정

 

  -Dspring.profiles.active="local"

개발이나 운영인 D빼고 설정

 

 

java에서 사용할 경우 @Value를 사용가능

@Value("#{config['db.username']}") 
private String dbUser;

@Value("#{프로퍼티ID['프로퍼티 내 선언 변수명']}") 
private String dbUser;

jsp에서 사용할 경우

<%taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<spring:eval var="commonLang" expression="@프로퍼티ID['프로퍼티 내 선언변수명']"/>

xml에서 사용할 경우

<property name="프로퍼티명" value="#{프로퍼티ID['프로퍼티 내 선언변수명']}" />

 

'백엔드' 카테고리의 다른 글

[springBoot] @Mapper 사용하기  (0) 2022.09.02
SOAP API 와 REST API의 차이점  (0) 2022.01.21

+ Recent posts