From b254d7e51875fac0473ec0ec9a659d129910c82b Mon Sep 17 00:00:00 2001 From: Emily Casey Date: Tue, 2 Jun 2020 15:16:21 -0400 Subject: [PATCH] Documents extention via custom factory Describes how to add support for additional service types and provides a simple example. Resolves #34 Signed-off-by: Emily Casey --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/README.md b/README.md index f9c17ed..18aa192 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,59 @@ Disable Property: `org.springframework.cloud.bindings.boot.wavefront.enable` | `management.metrics.export.wavefront.api-token` | `{secret/api-token}` | `management.metrics.export.wavefront.uri` | `{secret/uri}` + +## Extending Spring Boot Configuration + +Consumers can extend the `BindingSpecificEnvironmentPostProcessor` with support for additional bindings by registering additional implementations of the `BindingsPropertiesProcessor`. + +Within the `process` method, custom processors should make desired modifications to the application properties, using the contents of the bindings to compute property values as appropriate. Custom processors are strongly encouraged to use the `kind` of each binding to filter for bindings intended for that processor. + +Below is an example that processes a single binding of `kind` `myservice`. If such a binding exists this processor sets `my.service.enabled=true` and sets `my.service.uri` to the value of `uri` found in the binding secret. + +``` +package com.example; + + +import org.springframework.cloud.bindings.Binding; +import org.springframework.cloud.bindings.Bindings; +import org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor; +import org.springframework.core.env.Environment; + +import java.util.List; +import java.util.Map; + +public final class MyServiceBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + + public static final String KIND = "myservice"; + + @Override + public void process(Environment environment, Bindings bindings, Map properties) { + if (!environment.getProperty("com.example.bindings.myservice.enable", Boolean.class, true)) { + return; + } + List myBindings = bindings.filterBindings(KIND); + if (myBindings.size() == 0) { + return; + } + properties.put("my.service.uri", myBindings.get(0).getSecret().get("uri")); + properties.put("my.service.enabled", true); + } + +} +``` + +These lines from the above example allow users to set `com.example.bindings.myservice.enable=false` to disable the processor entirely: +``` + if (!environment.getProperty("com.example.bindings.myservice.enable", Boolean.class, true)) { + return; + } +``` + +You must add an entry in `META_INF/spring.factories` so that your custom processor can be discovered. +``` +org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=com.example.MyServiceBindingsPropertiesProcessor +``` + ## License This buildpack is released under version 2.0 of the [Apache License][a].