Skip to content

Commit

Permalink
Register a new class to resolve properties during bootstrap (spring-c…
Browse files Browse the repository at this point in the history
…loud#2375)

Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com>
  • Loading branch information
Ryan Baxter and ryanjbaxter authored Jan 23, 2024
1 parent c0d0851 commit 0805e5f
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.Supplier;

import org.apache.commons.logging.Log;

Expand Down Expand Up @@ -204,6 +205,9 @@ public List<ConfigServerConfigDataResource> resolveProfileSpecific(
return factory.create();
});

bootstrapContext.registerIfAbsent(PropertyResolver.class,
context -> new PropertyResolver(resolverContext.getBinder(), getBindHandler(resolverContext)));

ConfigServerConfigDataResource resource = new ConfigServerConfigDataResource(properties, location.isOptional(),
profiles);
resource.setProfileSpecific(!ObjectUtils.isEmpty(profiles));
Expand Down Expand Up @@ -273,4 +277,29 @@ private class PropertyHolder {

}

public static class PropertyResolver {

private final Binder binder;

private final BindHandler bindHandler;

public PropertyResolver(Binder binder, BindHandler bindHandler) {
this.binder = binder;
this.bindHandler = bindHandler;
}

public <T> T get(String key, Class<T> type, T defaultValue) {
return binder.bind(key, Bindable.of(type)).orElse(defaultValue);
}

public <T> T resolveConfigurationProperties(String prefix, Class<T> type, Supplier<T> defaultValue) {
return binder.bind(prefix, Bindable.of(type), bindHandler).orElseGet(defaultValue);
}

public <T> T resolveOrCreateConfigurationProperties(String prefix, Class<T> type) {
return binder.bindOrCreate(prefix, Bindable.of(type), bindHandler);
}

}

}

0 comments on commit 0805e5f

Please sign in to comment.