From 8f8f7d49205f11f0b93aa6e7846bf8eb26a0e854 Mon Sep 17 00:00:00 2001 From: spencergibb Date: Mon, 10 Aug 2020 15:27:17 -0400 Subject: [PATCH] Adds spring-cloud-starter-bootstrap. This allows users to opt-in to bootstrap via classpath rather than properties. --- pom.xml | 1 + spring-cloud-commons-dependencies/pom.xml | 5 +++ .../util/ConditionalOnBootstrapDisabled.java | 6 +++ .../util/ConditionalOnBootstrapEnabled.java | 6 +++ .../cloud/util/PropertyUtils.java | 24 +++++++++- spring-cloud-starter-bootstrap/pom.xml | 36 +++++++++++++++ .../cloud/bootstrap/marker/Marker.java | 29 ++++++++++++ .../cloud/bootstrap/marker/MarkerTests.java | 45 +++++++++++++++++++ .../src/test/resources/bootstrap.properties | 1 + 9 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-starter-bootstrap/pom.xml create mode 100644 spring-cloud-starter-bootstrap/src/main/java/org/springframework/cloud/bootstrap/marker/Marker.java create mode 100644 spring-cloud-starter-bootstrap/src/test/java/org/springframework/cloud/bootstrap/marker/MarkerTests.java create mode 100644 spring-cloud-starter-bootstrap/src/test/resources/bootstrap.properties diff --git a/pom.xml b/pom.xml index 99f1eb82..648979c9 100644 --- a/pom.xml +++ b/pom.xml @@ -161,6 +161,7 @@ spring-cloud-commons spring-cloud-loadbalancer spring-cloud-starter + spring-cloud-starter-bootstrap spring-cloud-starter-loadbalancer docs diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index bd2efb13..b7a6e210 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -54,6 +54,11 @@ spring-cloud-starter ${project.version} + + org.springframework.cloud + spring-cloud-starter-bootstrap + ${project.version} + org.springframework.cloud spring-cloud-starter-loadbalancer diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapDisabled.java b/spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapDisabled.java index 23979062..626225d0 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapDisabled.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapDisabled.java @@ -22,6 +22,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; import org.springframework.context.annotation.Conditional; @@ -41,6 +42,11 @@ public @interface ConditionalOnBootstrapDisabled { super(ConfigurationPhase.REGISTER_BEAN); } + @ConditionalOnClass(name = "org.springframework.cloud.bootstrap.marker.Marker") + static class OnBootstrapMarkerClassPresent { + + } + @ConditionalOnProperty(name = USE_LEGACY_PROCESSING_PROPERTY) static class OnUseLegacyProcessingEnabled { diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapEnabled.java b/spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapEnabled.java index d0f16ee9..23c7faa3 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapEnabled.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/util/ConditionalOnBootstrapEnabled.java @@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Conditional; @@ -41,6 +42,11 @@ public @interface ConditionalOnBootstrapEnabled { super(ConfigurationPhase.REGISTER_BEAN); } + @ConditionalOnClass(name = PropertyUtils.MARKER_CLASS) + static class OnBootstrapMarkerClassPresent { + + } + @ConditionalOnProperty(name = USE_LEGACY_PROCESSING_PROPERTY) static class OnUseLegacyProcessingEnabled { diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/util/PropertyUtils.java b/spring-cloud-context/src/main/java/org/springframework/cloud/util/PropertyUtils.java index 55c65af7..1565057a 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/util/PropertyUtils.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/util/PropertyUtils.java @@ -17,6 +17,7 @@ package org.springframework.cloud.util; import org.springframework.core.env.Environment; +import org.springframework.util.ClassUtils; public abstract class PropertyUtils { @@ -30,12 +31,33 @@ public abstract class PropertyUtils { */ public static final String USE_LEGACY_PROCESSING_PROPERTY = "spring.config.use-legacy-processing"; + /** + * Property name for bootstrap marker class name. + */ + public static final String MARKER_CLASS = "org.springframework.cloud.bootstrap.marker.Marker"; + + /** + * Boolean if bootstrap marker class exists. + */ + public static final boolean MARKER_CLASS_EXISTS = markerClassExists(); + + private static boolean markerClassExists() { + try { + ClassUtils.forName(MARKER_CLASS, null); + return true; + } + catch (ClassNotFoundException e) { + return false; + } + } + private PropertyUtils() { } public static boolean bootstrapEnabled(Environment environment) { - return environment.getProperty(BOOTSTRAP_ENABLED_PROPERTY, Boolean.class, false); + return environment.getProperty(BOOTSTRAP_ENABLED_PROPERTY, Boolean.class, false) + || MARKER_CLASS_EXISTS; } public static boolean useLegacyProcessing(Environment environment) { diff --git a/spring-cloud-starter-bootstrap/pom.xml b/spring-cloud-starter-bootstrap/pom.xml new file mode 100644 index 00000000..be3dcba9 --- /dev/null +++ b/spring-cloud-starter-bootstrap/pom.xml @@ -0,0 +1,36 @@ + + + + org.springframework.cloud + spring-cloud-commons-parent + 3.0.0-SNAPSHOT + .. + + jar + 4.0.0 + spring-cloud-starter-bootstrap + spring-cloud-starter-bootstrap + Spring Cloud Starter Bootstrap + https://projects.spring.io/spring-cloud + + Pivotal Software, Inc. + https://www.spring.io + + + ${basedir}/../.. + + + + + org.springframework.cloud + spring-cloud-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + \ No newline at end of file diff --git a/spring-cloud-starter-bootstrap/src/main/java/org/springframework/cloud/bootstrap/marker/Marker.java b/spring-cloud-starter-bootstrap/src/main/java/org/springframework/cloud/bootstrap/marker/Marker.java new file mode 100644 index 00000000..bde3c6f7 --- /dev/null +++ b/spring-cloud-starter-bootstrap/src/main/java/org/springframework/cloud/bootstrap/marker/Marker.java @@ -0,0 +1,29 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.bootstrap.marker; + +/** + * A marker class, so that, if present, spring cloud bootstrap is enable similar to how + * 'spring.cloud.bootstrap.enabled=true' works. + */ +public abstract class Marker { + + private Marker() { + + } + +} diff --git a/spring-cloud-starter-bootstrap/src/test/java/org/springframework/cloud/bootstrap/marker/MarkerTests.java b/spring-cloud-starter-bootstrap/src/test/java/org/springframework/cloud/bootstrap/marker/MarkerTests.java new file mode 100644 index 00000000..4c702798 --- /dev/null +++ b/spring-cloud-starter-bootstrap/src/test/java/org/springframework/cloud/bootstrap/marker/MarkerTests.java @@ -0,0 +1,45 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.bootstrap.marker; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.env.Environment; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +public class MarkerTests { + + @Autowired + private Environment env; + + @Test + public void testBootstrapIsEnabled() { + assertThat(env.getProperty("my.prop")).isEqualTo("my.value"); + } + + @SpringBootConfiguration + @EnableAutoConfiguration + protected static class TestConfig { + + } +} diff --git a/spring-cloud-starter-bootstrap/src/test/resources/bootstrap.properties b/spring-cloud-starter-bootstrap/src/test/resources/bootstrap.properties new file mode 100644 index 00000000..b8fdf64d --- /dev/null +++ b/spring-cloud-starter-bootstrap/src/test/resources/bootstrap.properties @@ -0,0 +1 @@ +my.prop=my.value \ No newline at end of file