Skip to content

topikachu/spring-propertysource-reload

Repository files navigation

PropertySource Reload Spring Framework Boot Starters

Table of Contents

Usage

This starter can auto detect the external property source (current only support java properties file) change on the file system, and triggers the refresh process of the application.

Repository

Repository contains:

  • propertysource-reload-spring-boot-starter the starter project which enables propertysource reload
  • propertysource-reload-example an example demonstrating the usage of the starter

Requirements and Downloads

Requirements:

  • Java 1.8
  • Spring Framework Boot > 2.x.x (web)

Gradle:

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
  implementation 'io.github.topikachu:propertysource-reload-spring-boot-starter:0.0.3'
}

Maven:

<dependency>
    <groupId>io.github.topikachu</groupId>
    <artifactId>propertysource-reload-spring-boot-starter</artifactId>
    <version>0.0.3</version>
</dependency>

How to enable external property source reloading

This starter requires the Spring Cloud Context . Add at least one Spring Cloud module, for example:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter</artifactId>
</dependency>

Specify the configuration file in the bootstrap.properties or bootstrap.yaml:

propertysource.reload.properties-files=config/foo.properties,config/bar.properties

Other available Spring Boot bootstrap configuration parameter:

propertysource.reload.poll-interval=5s
propertysource.reload.strategy=refresh_environment
propertysource.reload.max-wait-for-shutdown=2s
propertysource.reload.ignore-resource-not-found=true
propertysource.reload.ignore-resource-load-error=true

Once the content of the configuration file specified in propertysource.reload.properties-files is change, the application is reload automatically.

Reload Strategy

There are four strategies

  1. refresh_scope
    This is the default strategy. Execute ContextRefresher.refresh().
  2. refresh_environment
    Execute ContextRefresher.refreshEnvironment().
  3. exit_application
    Execute SpringApplication.exit()
  4. exit_application_force
    Execute SpringApplication.exit() and System.exit()

PropertySourceReloadEvent

There's a PropertySourceReloadEvent fired after each reload. To receive this event

	@EventListener(PropertySourceReloadEvent.class)
	public void onRefresh(PropertySourceReloadEvent event) {
		System.out.println(String.join(",", event.getKeys()));
		System.out.println(event.getFile().getName());
		System.out.println(event.getFileEvent());
	}

Environment Changes

Please read spring documentation for detail.
In a short description, the @ConfigurationProperties beans are rebind, but @Value are not by default. Any bean using @Value to inject the configuration must use @RefreshScope to receive the new change.

Delete Configuration at Runtime

It's very difficult to handle the configuration deletion. Try to always keep the keys at runtime. Another solution is to use "@RefreshScope" at any bean depending on the configuration may change during runtime.

Contributions

Contributions are welcome. Please respect the Code of Conduct.

Licenses

propertysource-reload-spring-boot-starter are licensed under the Apache 2.0 License. See LICENSE for details.

About

Spring Boot Starter to reload external configuration automaticlly

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages