복붙노트

[SPRING] Spring으로 속성을 재로드하는 방법?

SPRING

Spring으로 속성을 재로드하는 방법?

Spring 3에서 속성 파일을 사용하고 있습니다. Spring은 컨텍스트를 초기화 할 때 속성 파일을로드하여 @Value 주석이있는 모든 빈에 배치합니다.

파일의 일부 속성을 업데이트하고 서버를 다시 시작하고 컨텍스트를 다시로드하지 않고 새 속성을 Spring으로 다시로드 할 서버에 JMX를 노출 할 수있는 권한을 원합니다.

속성을 다시로드하고 모든 빈에 채우는 Spring 메서드를 사용하여이를 구현할 수 있습니까? 아니면 내 자신으로 이런 식으로 작성해야합니까?

해결법

  1. ==============================

    1.java.util.Properties를 Apache Commons Configuration 프로젝트의 PropertiesConfiguration으로 대체 할 것을 제안합니다. 파일이 언제 변경되는지 감지하거나 JMX를 통해 트리거하여 자동 재로드를 지원합니다.

    java.util.Properties를 Apache Commons Configuration 프로젝트의 PropertiesConfiguration으로 대체 할 것을 제안합니다. 파일이 언제 변경되는지 감지하거나 JMX를 통해 트리거하여 자동 재로드를 지원합니다.

  2. ==============================

    2.나는 그것을하는 일반적인 방법이 없다고 생각한다. 가장 "깨끗한"것은 Spring 컨텍스트를 종료하고 처음부터 다시 빌드하는 것입니다. 예를 들어, DBCP 연결 풀 사용 및 데이터베이스 연결 URL 업데이트를 고려하십시오. 즉, 풀을 제대로 종료해야하고 새 개체를 만들어야하며 풀에 대한 모든 참조를 업데이트해야합니다. 이제 일부 빈이 해당 풀에서 연결을 가져올 수 있으며 풀 구성을 업데이트하면 어떻게 든 연결을 다시 요청해야합니다. 따라서 콩은 그것을하는 방법을 알아야 할 수도 있습니다. 이는 일반적이지 않습니다.

    나는 그것을하는 일반적인 방법이 없다고 생각한다. 가장 "깨끗한"것은 Spring 컨텍스트를 종료하고 처음부터 다시 빌드하는 것입니다. 예를 들어, DBCP 연결 풀 사용 및 데이터베이스 연결 URL 업데이트를 고려하십시오. 즉, 풀을 제대로 종료해야하고 새 개체를 만들어야하며 풀에 대한 모든 참조를 업데이트해야합니다. 이제 일부 빈이 해당 풀에서 연결을 가져올 수 있으며 풀 구성을 업데이트하면 어떻게 든 연결을 다시 요청해야합니다. 따라서 콩은 그것을하는 방법을 알아야 할 수도 있습니다. 이는 일반적이지 않습니다.

    구성 및 업데이트 이벤트가있는 별도의 bean을 작성하고 구성 변경 사항에 대해 알아야하는 모든 bean에 대한 종속성으로 해당 bean을 두는 것이 좋습니다. 그런 다음 Apache Commons Configuration을 사용하여 파일 변경 사항을 추적하고 구성 업데이트를 전파 할 수 있습니다.

    아마도 JMS를 사용하는 것이 좋습니다 (나중에 앱을 배포하는 경우).

  3. ==============================

    3.예, Spring JMX 방식으로이 작업을 수행 할 수 있습니다. Spring 설정 파일에이 빈을 추가하십시오. 별도의 메소드를 작성하여 특성 파일을 읽으십시오. 이 샘플에서는 callThisAgain () 메서드를 사용합니다.

    예, Spring JMX 방식으로이 작업을 수행 할 수 있습니다. Spring 설정 파일에이 빈을 추가하십시오. 별도의 메소드를 작성하여 특성 파일을 읽으십시오. 이 샘플에서는 callThisAgain () 메서드를 사용합니다.

    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
        <property name="beans">
            <map>
                <entry key="your.package.bean:name=sampleBeanService" value-ref="sampleService"/>
            </map>
        </property>
        <property name="assembler">
            <bean class="org.springframework.jmx.export.assembler.MethodNameBasedMBeanInfoAssembler">
                <property name="managedMethods">
                    <value>
                        callThisAgain <!--Simply declare the method name here (only the name) -->
                    </value>
                </property>
            </bean>
        </property>
    </bean>
    
    <bean class="org.springframework.jmx.support.ConnectorServerFactoryBean" depends-on="rmiRegistry">
        <property name="objectName" value="connector:name=rmi"/>
        <property name="serviceUrl" value="service:jmx:rmi://127.0.0.1/jndi/rmi://127.0.0.1:11000/sample"/>
    </bean>
    
    <bean id="rmiRegistry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
        <property name="port" value="11000"/>
    </bean>
    

    그 후 jconsole을 사용하여 서버를 다시 시작하지 않고도 메소드를 다시로드 할 수 있습니다.

  4. ==============================

    4.다음과 같이 스프링과 함께 아파치를 사용하십시오 :

    다음과 같이 스프링과 함께 아파치를 사용하십시오 :

    @Component
    public class ApplicationProperties {
        private PropertiesConfiguration configuration;
    
        @PostConstruct
        private void init() {
            try {
                String filePath = "/opt/files/myproperties.properties";
                System.out.println("Loading the properties file: " + filePath);
                configuration = new PropertiesConfiguration(filePath);
    
                //Create new FileChangedReloadingStrategy to reload the properties file based on the given time interval
                FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
               fileChangedReloadingStrategy.setRefreshDelay(60*1000);
                configuration.setReloadingStrategy(fileChangedReloadingStrategy);
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
        }
    
        public String getProperty(String key) {
            return (String) configuration.getProperty(key);
        }
    
        public void setProperty(String key, Object value) {
            configuration.setProperty(key, value);
        }
    
        public void save() {
            try {
                configuration.save();
            } catch (ConfigurationException e) {
                e.printStackTrace();
            }
        }
    }
    
  5. ==============================

    5.

    <bean id="propertiesReloadingStrategy" class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy">
        <property name="refreshDelay" value="30000" /> <!-- 30 seconds -->
    </bean>
    
    <bean id="reloadableProperties" class="org.apache.commons.configuration.PropertiesConfiguration">
        <constructor-arg value="file:/web/${weblogic.Domain}/${weblogic.Name}/${app.Name}/reloadable_cfg/Reloadable.properties"/>
        <property name="reloadingStrategy" ref="propertiesReloadingStrategy"/>
    </bean>
    
  6. ==============================

    6.여기 몇몇은 속성을 사용하기위한 외부적인 방법 (스프링의 고유 한 속성 파일 사용 방법의 외부)을 사용하도록 제안하고 있습니다. 이 답변은 Spring Boot와 Java EE에서 https://stackoverflow.com/a/52648630/39998 Hot Reloading 속성을 찾고있는 것과 정확히 일치합니다.

    여기 몇몇은 속성을 사용하기위한 외부적인 방법 (스프링의 고유 한 속성 파일 사용 방법의 외부)을 사용하도록 제안하고 있습니다. 이 답변은 Spring Boot와 Java EE에서 https://stackoverflow.com/a/52648630/39998 Hot Reloading 속성을 찾고있는 것과 정확히 일치합니다.

  7. from https://stackoverflow.com/questions/13248066/how-to-reload-properties-with-spring by cc-by-sa and MIT license