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">
        <constructor-arg type="java.lang.String"  value="c://parameter.properties" />
        <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;
    }
     
}


Posted by 타다키치
,