1. pom.xml 파일에 다음을 추가
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>
이후에 혹시 에러가 나면 다음 링크를 참고하자.
http://stackoverflow.com/questions/9928574/what-going-wrong-in-using-propertiesconfiguration
http://commons.apache.org/proper/commons-configuration/dependencies_1_10.html
런타임 시 dependency 문제로 버그가 날 수 있다.
2. 빈 등록
1 2 3 4 5 6 7 8 | <!-- bean for loading properties dynamically --> <bean id= "propertiesConfiguration" class = "org.apache.commons.configuration.PropertiesConfiguration" > <property name= "reloadingStrategy" ref= "fileChangedReloadingStrategy" /> </bean> <bean id= "fileChangedReloadingStrategy" class = "org.apache.commons.configuration.reloading.FileChangedReloadingStrategy" /> |
value 부분을 유심히 봐주길 바란다. 저기에 'c://' 없이 파일 이름만 쓰면 클래스패스에서 읽어온다.
-해당 디렉토리에 properties 파일을 가져다 놓는다. 각자 서버에 맞게 디렉토리를 정해주면 될 것이다.
-사용 시에는 @autowired로 빈을 불러온 뒤 메소드로 호출해야 한다.
파일이 수정됐을 때 메소드를 따로 호출하거나, 파일을 강제로 다시 읽게 할 수 있다.
3. 사용 예
만일 DB에서 읽어온 데이터가 Ehcache로 캐싱되어 있는데, 어떠한 프로퍼티가 바뀌면 캐싱된 데이터를 자동으로 지우고 다시 캐싱을 해야한다고 해보자.
이럴 때는 웹 어플리케이션에 요청이 들어올 때 마다 프로퍼티 파일이 바뀌었는지 검사한 후, 바뀌었다면 캐시를 지우게 하는 것도 한 방법일 것이다.
스프링의 HandlerInterceptorAdapter를 이때 사용하면 좋다.
아래는 예제 코드다. 주석 내용을 참고하면 쉽게 이해가 될 것이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.vp.util; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.vp.mapper.UserMapper; public class FileChangedReloadingInterceptor extends HandlerInterceptorAdapter{ @Autowired UserMapper mapper; @Autowired FileChangedReloadingStrategy fileChangedReloadingStrategy; @Autowired EhCacheCacheManager cacheManager; @Autowired PropertiesConfiguration propertiesConfiguration; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //delete cache and refresh properties if file was changed if ( this .fileChangedReloadingStrategy.reloadingRequired()){ //delete all cache entries of each Cache this .cacheManager.getCacheManager().getCache( "stat" ).removeAll(); this .cacheManager.getCacheManager().getCache( "chartType" ).removeAll(); //refresh properties this .propertiesConfiguration.refresh(); } return true ; } } |
'코딩 > Spring' 카테고리의 다른 글
하이버네이트 Hibernate ORM 스프링 Spring JPA 쓸 때 유의할 점 (0) | 2015.12.22 |
---|---|
Hibernate 1 - 스프링 Spring data jpa로 하이버네이트 Hibernate 적용하기 (0) | 2015.12.15 |
EhCache 안에 Key 모두 확인하는 법 (0) | 2015.12.07 |
이니시스 incis 결제시스템 스프링 Spring 에 붙일 때 고려 사항 (0) | 2015.10.22 |
Spring Ehcache 쓸 때 유의할 점 (0) | 2015.10.09 |