Skip to content

Instantly share code, notes, and snippets.

@mumrah
Created September 4, 2013 16:43
Show Gist options
  • Save mumrah/6439588 to your computer and use it in GitHub Desktop.
Save mumrah/6439588 to your computer and use it in GitHub Desktop.
Example of configuring Archaius through Spring XML. Package names have been removed, so this probably won't work without some modification.
import org.apache.commons.configuration.AbstractConfiguration;
import com.netflix.config.ConcurrentCompositeConfiguration;
import java.util.ArrayList;
import java.util.List;
public class ConcurrentCompositeConfigurationFactory {
List<AbstractConfiguration> configs = new ArrayList<AbstractConfiguration>();
public void setConfigs(List<AbstractConfiguration> configs) {
this.configs.clear();
this.configs.addAll(configs);
}
public List<AbstractConfiguration> getConfigs() {
return this.configs;
}
public ConcurrentCompositeConfiguration create() {
ConcurrentCompositeConfiguration instance = new ConcurrentCompositeConfiguration();
for(AbstractConfiguration config : this.configs) {
instance.addConfiguration(config);
}
return instance;
}
}
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.beans.factory.annotation.Autowired;
public class ConfigurationResources implements ResourceLoaderAware {
private ResourceLoader resourceLoader;
private List<String> resources = new ArrayList<String>();
@Override
@Autowired
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public void setResources(List<String> resources) {
this.resources.clear();
this.resources.addAll(resources);
}
public List<String> getResources() {
return new ArrayList<String>(this.resources);
}
public URL[] convertToURLs() throws IOException {
URL[] urls = new URL[resources.size()];
for(int i=0; i<resources.size(); i++) {
Resource resource = resourceLoader.getResource(resources.get(i));
urls[i] = resource.getURL();
}
return urls;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:prop="http://camel.apache.org/schema/placeholder"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd">
<!-- For auto-wiring -->
<context:annotation-config/>
<!--
Dynamic configuration management using local properties files, and System properties
TODO some description
-->
<!-- Example bean for configResources using classpath properties file -->
<bean id="configResources" class="ConfigurationResources">
<property name="resources">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<!-- Properties file dynamic config. Callers of this file must provide configResources -->
<bean id="propertiesDynamicConfig" class="com.netflix.config.DynamicConfiguration">
<constructor-arg>
<bean class="com.netflix.config.sources.URLConfigurationSource">
<constructor-arg>
<bean factory-bean="configResources" factory-method="convertToURLs"/>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="com.netflix.config.FixedDelayPollingScheduler">
<constructor-arg type="int"><value>1000</value></constructor-arg>
<constructor-arg type="int"><value>2000</value></constructor-arg>
<constructor-arg type="boolean"><value>true</value></constructor-arg>
</bean>
</constructor-arg>
</bean>
<!-- Composite configuration -->
<bean id="configFactory" class="ConcurrentCompositeConfigurationFactory">
<property name="configs">
<list>
<bean class="org.apache.commons.configuration.SystemConfiguration"/>
<ref bean="propertiesDynamicConfig"/>
</list>
</property>
</bean>
<bean id="config" factory-bean="configFactory" factory-method="create"/>
<!-- Register our configuration with JMX
<bean class="com.netflix.config.jmx.ConfigJMXManager" factory-method="registerConfigMbean">
<constructor-arg ref="config"/>
</bean>
-->
<!-- Convert our composite configuration to bridged property placeholder -->
<bean class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="properties">
<bean class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties">
<constructor-arg ref="config"/>
</bean>
</property>
</bean>
</beans>
@bmosher1
Copy link

bmosher1 commented Oct 7, 2015

Hello,

How did you create the DynamicConfiguration Class in this example? I get an error at runtime that com.netflix.config.DynamicConfiguration does not exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment