Java
[Java] PropertyPlaceholderConfigurer로 properties 파일 읽어오기
Jane Kwon
2021. 4. 12. 11:00
반응형
html에서 ${}을 사용해서 config 파일에 있는 값을 불러오길래 찾아봤더니,
servlet-context.xml에서 해당 설정이 되어 있는 걸 발견하고 방법을 찾아보았다.
PropertyPlaceholderConfigurer을 사용해서 설정 가능하다.
location 프로퍼티의 값에 콤마나 공백으로 구분된 properties 파일 목록을 넣고,
파일에 포함된 프로퍼티의 값은 ${프로퍼티 이름} 형식으로 사용 가능하다.
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location" value="/WEB-INF/config/config.properties"/>
<beans:property name="fileEncoding" value="UTF-8" />
</beans:bean>
한 개 이상의 properties 파일을 불러와야할 때는 리스트로 묶어서 호출 가능하다.
아래 예제는 실제로 우리 소스에 구현되어 있는 내용이다.
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="locations">
<beans:list>
<beans:value>classpath:/config/config-common.properties</beans:value>
<beans:value>classpath:/config/config-#{systemProperties['spring.profiles.active']}.properties</beans:value>
</beans:list>
</beans:property>
</beans:bean>
systemProperties를 이용해서 현재 실행 중인 소스가
local인지, dev인지, staging인지, prod인지 값을 가져와서 해당 properties 파일을 읽어온다.
설정이 완료되면 HTML에서도 이렇게 ${}을 사용해서 프로퍼티 값을 가져와 사용할 수 있다.
<script type="text/javascript" src="${resource}/js/jquery/jquery-2.1.4.min.js"></script>
(참고 : ktko.tistory.com/entry/Spring-properties-%EC%9D%BD%EC%96%B4%EC%98%A4%EA%B8%B0)
반응형