[springframework] confing properties re-loading 기능

2014. 1. 17. 21:13OpenSource/Spring MVC

반응형

 

 

[springframework] confing properties re-loading 기능

 

1. 배 경

2. 구 현

3. 결 과 

4. 참조 사이트


<< 배 경 >>

Spring을 사용하면서 config properties 파일이 WAS의 재가동 없이 반영 되어야 하는 경우가 있다.

찾아보았더니 org.apache.commons.configuration.PropertiesConfiguration를 통해서 구현 할 수 었다!!


<< 구 현 >>

   1) spring의 context-properties.xml(properties location이 있는 context) 에서 bean을 만든다.

   - context-properties.xml 부분

     <context:property-placeholder location="classpath:properties/config.properties"/>  


     이 부분 추가!! value에서 path부분에서 너무 많은 소요를 하였다..ㅠ.ㅠ...classpath: 를 넣었었다...

     <bean id="runtimeProperties" class="org.apache.commons.configuration.PropertiesConfiguration">            <constructor-arg type="java.lang.String"  value="properties/config.properties" />

      <property name="reloadingStrategy" ref="reloadingStrategy" />

</bean>


    <bean id="reloadingStrategy" 

          class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy" />    


  - bean이 등록이 되었다면(WAS 기동 시 잘 읽어들인다면), @Autowired를 하여 사용 하면 된다.

    @Autowired

    private PropertiesConfiguration runtimeProperties;


    public String getConfigProperites() {

        return (String)runtimeProperties.getProperty("config.test.acet");

    }



<< 결과 >>

테스트로 2가지를 해보았다.

@Value와 @Autowired private PropertiesConfiguration  2가지 버전으로 테스트를 하였다.

  [ 결과 ]

   getConfigProperites()=>486486

   value--->1000004


config.properties의 내용은 아래와 같다.

   # properties test

   config.test.acet=1000004


소스에서는 

Value는 @Value("${config.test.acet}")

             private String name;  로 가져와서 찍어주었고,

PropertiesConfiguration 는 위의 소스 처럼 가져와서 찍어주었다.


System.out.println("getConfigProperites()=>"+getConfigProperites());

System.out.println("value--->"+name);


WAS는 Tomcat 6.0을 사용하였고, local 에서 테스트를 위해 아래와 같이 설정을 해주어야 한다.

단, server.xml에서 다음과 같이 수정을 해줘야 한다.(very important~!!!)

<Host appBase="webapps" autoDeploy="false" name="10.214.184.86" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">


<Context docBase="ktdsOssDevPlay" path="/" reloadable="false" source="org.eclipse.jst.jee.server:ktdsOssDevPlay"/></Host> 


그런 다음 tomcat에서 Use workspace metadata를 사용하였으며 (Server 찍고~ 마우스 우클릭에서 Properties에서 선택 가능 ) 

C:\ACET\workingSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\ktdsOssDevPlay\WEB-INF\classes\properties에서 즉, 배포 된 녀석(config.properties)을 수정하였다. 486486으로^-^/


참조 사이트는 너무 이것저것 봐서...대충 띄워져있는 것만 써놨다. 그냥 구글링을 하시면 됩니다~


<< 참조 사이트 >>

1) http://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration/PropertiesConfiguration.html#PropertiesConfiguration(java.io.File)


2) https://jira.springsource.org/browse/SPR-4714


3) http://blog.naver.com/PostView.nhn?blogId=leehyogun&logNo=160766551


4) http://goodwilldd.blogspot.kr/2012/09/spring-properties-reload_13.html


아! PropertiesConfiguration.class를 보면 아래와 같다.

public class PropertiesConfiguration extends AbstractFileConfiguration { 블라블라~중에서 

발췌한 내용이다. 여기서 보아야 할 것은 PropertiesConfiguration의 bean을 만들 때 생성자를 만드는 부분이 있는데 그 생성자를 만들 때 어떻게 생성 하는지이다.

File, String, Url 등등 을 파라미터로 받아서 생성이 가능하다는 것이다.




                               - END - 




반응형