io.github.topikachu:propertysource-reload-spring-boot-starter

propertysource reload spring boot starter

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

io.github.topikachu
ArtifactId

ArtifactId

propertysource-reload-spring-boot-starter
Last Version

Last Version

0.0.7
Release Date

Release Date

Type

Type

jar
Description

Description

io.github.topikachu:propertysource-reload-spring-boot-starter
propertysource reload spring boot starter
Project URL

Project URL

https://github.com/topikachu/spring-propertysource-reload
Project Organization

Project Organization

Pivotal Software, Inc.
Source Code Management

Source Code Management

https://github.com/topikachu/spring-propertysource-reload/tree/master/propertysource-reload-spring-boot-starter

Download propertysource-reload-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.topikachu/propertysource-reload-spring-boot-starter/ -->
<dependency>
    <groupId>io.github.topikachu</groupId>
    <artifactId>propertysource-reload-spring-boot-starter</artifactId>
    <version>0.0.7</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.topikachu/propertysource-reload-spring-boot-starter/
implementation 'io.github.topikachu:propertysource-reload-spring-boot-starter:0.0.7'
// https://jarcasting.com/artifacts/io.github.topikachu/propertysource-reload-spring-boot-starter/
implementation ("io.github.topikachu:propertysource-reload-spring-boot-starter:0.0.7")
'io.github.topikachu:propertysource-reload-spring-boot-starter:jar:0.0.7'
<dependency org="io.github.topikachu" name="propertysource-reload-spring-boot-starter" rev="0.0.7">
  <artifact name="propertysource-reload-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.topikachu', module='propertysource-reload-spring-boot-starter', version='0.0.7')
)
libraryDependencies += "io.github.topikachu" % "propertysource-reload-spring-boot-starter" % "0.0.7"
[io.github.topikachu/propertysource-reload-spring-boot-starter "0.0.7"]

Dependencies

compile (5)

Group / Artifact Type Version
commons-io : commons-io jar 2.7
org.projectlombok : lombok Optional jar 1.18.12
org.reflections : reflections jar 0.9.12
org.springframework.boot : spring-boot-configuration-processor Optional jar
junit : junit jar

provided (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar
org.springframework.cloud : spring-cloud-context jar
com.github.spotbugs : spotbugs-annotations jar 4.0.4

test (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar
org.springframework.boot : spring-boot-starter-test jar
org.apache.commons : commons-lang3 jar 3.10

Project Modules

There are no modules declared in this project.

PropertySource Reload Spring Framework Boot Starters

Java CI with Maven

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

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

  1. 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
  2. Specify the configuration file in the bootstrap.properties or bootstrap.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

  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()
  5. restart
    Set management.endpoint.restart.enabled=true to enable this strategy. See RestartEndpoint#restart for 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.

Versions

Version
0.0.7
0.0.6
0.0.4
0.0.3
0.0.2