Skip to content

Dynocon for Spring Boot

Eugene Strokin edited this page Feb 3, 2021 · 2 revisions

You don't need to do anything special when using the default configuration sources. But modern Spring Boot applications are usually running in environments that require specific, not default, configuration source, AWS DynamoDB table for example. In this case, the source needs to be registered at the earliest stage of Spring Boot execution:

public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.addListeners(new DynamodbPropertiesInitializer());
        application.run(args);
    }
}

Don't forget to add the dependency which provides AWS DynamoDB configuration source:

<dependency>
	<groupId>com.comcast</groupId>
	<artifactId>dynocon-dynamodb</artifactId>
	<version>LATEST</version>
</dependency>

Now, all you need it to register the configuration source as following:

import com.comcast.dynocon.SourcesFactory;
import com.comcast.dynocon.dynamodb.DynamodbPropertiesSource;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;

public class DynamodbPropertiesInitializer implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {

    @Override
    public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
        String sources = event.getEnvironment().getProperty(SourcesFactory.PARAM_SOURCES);
        if (sources != null) {
            System.setProperty(SourcesFactory.PARAM_SOURCES, sources);
        }
        SourcesFactory.instance.registerSourceAlias("dynamodb", new DynamodbPropertiesSource());
    }
}

Now you have a new configuration source registered. The configuration source would poll the properties from a DynamoDB table named config (the name could be changed). The records in your DynamoDB table will be the properties the source will provide.

You can let the Dynocon know to use the configuration source by adding a line into your Spring Boot application.properties file:

dynocon.sources=env,sys,dynamodb

Dynocon will use Environment Variables (source with name env), System Variables (source with name sys), and AWS DynamoDB Table (source with name dynamodb) which you just registered and the Spring Boot startup, as the sources for the configuration properties.

You can add or update DynamoDB records to make changes for the configuration properties, and the application will see the change without application restart.

Clone this wiki locally