diff --git a/README.adoc b/README.adoc index a17b4302..363959a5 100644 --- a/README.adoc +++ b/README.adoc @@ -1,4 +1,8 @@ -// Do not edit this file (e.g. go instead to src/main/asciidoc) +//// +DO NOT EDIT THIS FILE. IT WAS GENERATED. +Manual changes to this file will be lost when it is generated again. +Edit the files in the src/main/asciidoc/ directory instead. +//// image::https://circleci.com/gh/spring-cloud/spring-cloud-commons.svg?style=svg[Build Status, link=https://circleci.com/gh/spring-cloud/spring-cloud-commons] diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java index e277b0c2..1f12d7b0 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2019 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. @@ -45,6 +45,7 @@ import org.springframework.context.annotation.Configuration; * {@link EnableAutoConfiguration Auto-configuration} for Spring Cloud Commons Client. * * @author Spencer Gibb + * @author Olga Maciaszek-Sharma */ @Configuration @AutoConfigureOrder(0) @@ -54,7 +55,7 @@ public class CommonsClientAutoConfiguration { @EnableConfigurationProperties(DiscoveryClientHealthIndicatorProperties.class) @ConditionalOnClass(HealthIndicator.class) @ConditionalOnBean(DiscoveryClient.class) - @ConditionalOnProperty(value = "spring.cloud.discovery.enabled", matchIfMissing = true) + @ConditionalOnDiscoveryEnabled protected static class DiscoveryLoadBalancerConfiguration { @Bean @ConditionalOnProperty(value = "spring.cloud.discovery.client.health-indicator.enabled", matchIfMissing = true) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/ConditionalOnDiscoveryEnabled.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/ConditionalOnDiscoveryEnabled.java new file mode 100644 index 00000000..c8ef5818 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/ConditionalOnDiscoveryEnabled.java @@ -0,0 +1,41 @@ +/* + * Copyright 2019-2019 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 + * + * http://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.client; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +/** + * Provides a more succinct conditional spring.cloud.discovery.enabled. + * + * @since 2.0 + * @author Olga Maciaszek-Sharma + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ConditionalOnProperty(value = "spring.cloud.discovery.enabled", matchIfMissing = true) +public @interface ConditionalOnDiscoveryEnabled { + +} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/CommonsClientAutoConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/CommonsClientAutoConfigurationTests.java index c11877f7..08407688 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/CommonsClientAutoConfigurationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/CommonsClientAutoConfigurationTests.java @@ -1,6 +1,23 @@ +/* + * Copyright 2016-2019 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 + * + * http://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.client; import org.junit.Test; + import org.springframework.beans.BeansException; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; @@ -10,6 +27,7 @@ import org.springframework.cloud.client.discovery.health.DiscoveryClientHealthIn import org.springframework.cloud.client.discovery.health.DiscoveryCompositeHealthIndicator; import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -22,6 +40,7 @@ import static org.junit.Assert.fail; /** * @author Spencer Gibb + * @author Olga Maciaszek-Sharma */ public class CommonsClientAutoConfigurationTests { @@ -69,6 +88,18 @@ public class CommonsClientAutoConfigurationTests { } } + @Test + public void conditionalOnDiscoveryEnabledWorks() { + try (ConfigurableApplicationContext context = init( + "spring.cloud.discovery.enabled=false")) { + assertBeanNonExistant(context, TestBean.class); + } + try (ConfigurableApplicationContext context = init( + "spring.cloud.discovery.enabled=true")) { + assertThat(context.getBean(TestBean.class), is(notNullValue())); + } + } + private void assertBeanNonExistant(ConfigurableApplicationContext ctxt, Class beanClass) { try { @@ -87,7 +118,22 @@ public class CommonsClientAutoConfigurationTests { @Configuration @EnableAutoConfiguration - @Import(NoopDiscoveryClientAutoConfiguration.class) + @Import({NoopDiscoveryClientAutoConfiguration.class, DiscoveryEnabledConfig.class}) protected static class Config { + } + + @Configuration + @ConditionalOnDiscoveryEnabled + protected static class DiscoveryEnabledConfig { + @Bean + TestBean testBean() { + return new TestBean(); + } + } + + private static class TestBean { + + } + }