PropertySource Reload Spring Framework Boot Starters
Table of Contents
- Usage
- Repository
- Requirements and Downloads
- How to enable external property source reloading
- Spring Boot bootstrap configuration parameter:
- Reload Strategy
- PropertySourceReloadEvent
- Environment Changes
- Delete Configuration at Runtime
- Contributions
- Licenses
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-starterthe starter project which enables propertysource reloadpropertysource-reload-examplean example demonstrating the usage of the starter
Requirements and Downloads
Requirements:
- Java 1.8
- Spring Framework Boot > 2.x.x
Gradle:
repositories {
jcenter()
mavenCentral()
}
dependencies {
implementation 'io.github.topikachu:propertysource-reload-spring-boot-starter:0.0.7'
}
Maven:
<dependency>
<groupId>io.github.topikachu</groupId>
<artifactId>propertysource-reload-spring-boot-starter</artifactId>
<version>0.0.7</version>
</dependency>
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>
How to enable external property source reloading
- Use annotation at class level
@ReloadablePropertySource({ "config/a.properties", "config/b.properties" })The starter only scans the packages same as or sub of the application's. It's NOT aware of@ComponentScan - Specify the configuration file in the
bootstrap.propertiesorbootstrap.yaml:
propertysource.reload.properties-files=config/foo.properties,config/bar.properties
Spring Boot bootstrap configuration parameter:
propertysource.reload.properties-files=
propertysource.reload.poll-interval=5s
propertysource.reload.strategy=refresh_environment
propertysource.reload.max-wait-for-shutdown=2s
propertysource.reload.max-wait-for-system-exit=10s
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
- refresh_scope
This is the default strategy. ExecuteContextRefresher.refresh(). - refresh_environment
ExecuteContextRefresher.refreshEnvironment(). - exit_application
ExecuteSpringApplication.exit() - exit_application_force
ExecuteSpringApplication.exit()andSystem.exit() - restart
Setmanagement.endpoint.restart.enabled=trueto enable this strategy. SeeRestartEndpoint#restartfor details.
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.