Backend Development 7 min read

Master Dynamic Configuration Refresh in Spring Cloud Without Nacos – Step‑by‑Step Guide

This article walks you through configuring Spring Cloud for dynamic property updates, covering dependency setup, enabling bootstrap, defining configuration properties, exposing a refresh endpoint, and listening to EnvironmentChangeEvent, all illustrated with complete code snippets and screenshots.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Dynamic Configuration Refresh in Spring Cloud Without Nacos – Step‑by‑Step Guide

1. Introduction

In a Spring Cloud micro‑service architecture, configuration management is essential. As services grow, configuration often needs to be adjusted dynamically so that applications can respond in real time. Spring Cloud offers several mechanisms to achieve dynamic updates.

2. Practical Example

2.1 Dependency Management

<code>&lt;spring-cloud.version&gt;2021.0.7&lt;/spring-cloud.version&gt;
&lt;dependencyManagement&gt;
  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
      &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
      &lt;version&gt;${spring-cloud.version}&lt;/version&gt;
      &lt;type&gt;pom&lt;/type&gt;
      &lt;scope&gt;import&lt;/scope&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;
&lt;/dependencyManagement&gt;</code>

The project also needs spring-cloud-context ; you can add spring-cloud-starter as shown below.

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
  &lt;artifactId&gt;spring-cloud-starter&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

2.2 Enabling Bootstrap Configuration

There are two ways to activate the default bootstrap.yml configuration:

First method – add the starter dependency:

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
  &lt;artifactId&gt;spring-cloud-starter-bootstrap&lt;/artifactId&gt;
&lt;/dependency&gt;</code>

Second method – launch the application with the JVM argument:

<code>-Dspring.cloud.bootstrap.enabled=true</code>

The key listener is BootstrapApplicationListener .

2.3 Initial Configuration

Example bootstrap.yml :

<code>pack:
  name: ooxx</code>

Corresponding properties class:

<code>@Component("pp")
@ConfigurationProperties(prefix = "pack")
public class PackProperties {
  private String name;
  public String getName() { return name; }
  public void setName(String name) { this.name = name; }
}</code>

Test controller:

<code>@RestController
@RequestMapping("/pack")
public class PackController {
  @Resource(name = "pp")
  private PackProperties props;
  @GetMapping("")
  public PackProperties p() { return props; }
}</code>

When the configuration file changes, the updated values are displayed as shown in the screenshot below.

2.4 Triggering Refresh

Inject ContextRefresher and expose a refresh endpoint:

<code>@Resource
private ContextRefresher contextRefresher;
@GetMapping("/refresh")
public Object refresh() { return this.contextRefresher.refresh(); }</code>

Calling this endpoint re‑initializes beans in the refresh scope and re‑binds @ConfigurationProperties classes, publishing an EnvironmentChangeEvent .

After modifying bootstrap.yml to:

<code>pack:
  name: ooxx</code>

and invoking /refresh , the new configuration is applied without restarting the service, as demonstrated in the following image.

2.5 Listening to Configuration Changes

Implement an ApplicationListener&lt;EnvironmentChangeEvent&gt; to react to updates:

<code>@Component
public class PackApplicationEventListener implements ApplicationListener<EnvironmentChangeEvent> {
  @Resource
  private Environment env;
  @Override
  public void onApplicationEvent(EnvironmentChangeEvent event) {
    System.out.println(event.getKeys());
    System.out.println(env.getProperty("pack.name"));
  }
}</code>

When the configuration changes, the listener prints the changed keys and the new property value, e.g.:

<code>[pack.name]
xxxooo</code>
backendJavaSpring BootSpring Clouddynamic-configConfiguration Refresh
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.