Browse Source

Add ConditionalOnDiscoveryEnabled. (#482)

* Add ConditionalOnDiscoveryEnabled.

* Fix typo.

* Fix typo.

* Add @since info. Fix typo.
pull/486/head
Olga Maciaszek-Sharma 6 years ago committed by GitHub
parent
commit
77fd62d4f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      README.adoc
  2. 5
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java
  3. 41
      spring-cloud-commons/src/main/java/org/springframework/cloud/client/ConditionalOnDiscoveryEnabled.java
  4. 48
      spring-cloud-commons/src/test/java/org/springframework/cloud/client/CommonsClientAutoConfigurationTests.java

6
README.adoc

@ -1,4 +1,8 @@ @@ -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]

5
spring-cloud-commons/src/main/java/org/springframework/cloud/client/CommonsClientAutoConfiguration.java

@ -1,5 +1,5 @@ @@ -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; @@ -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 { @@ -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)

41
spring-cloud-commons/src/main/java/org/springframework/cloud/client/ConditionalOnDiscoveryEnabled.java

@ -0,0 +1,41 @@ @@ -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 <code>spring.cloud.discovery.enabled</code>.
*
* @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 {
}

48
spring-cloud-commons/src/test/java/org/springframework/cloud/client/CommonsClientAutoConfigurationTests.java

@ -1,6 +1,23 @@ @@ -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 @@ -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; @@ -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 { @@ -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 { @@ -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 {
}
}

Loading…
Cancel
Save