diff --git a/spring-cloud-consul-binder/src/main/java/org/springframework/cloud/consul/binder/config/ConsulBinderConfiguration.java b/spring-cloud-consul-binder/src/main/java/org/springframework/cloud/consul/binder/config/ConsulBinderConfiguration.java index 82e8236e..8ad37257 100644 --- a/spring-cloud-consul-binder/src/main/java/org/springframework/cloud/consul/binder/config/ConsulBinderConfiguration.java +++ b/spring-cloud-consul-binder/src/main/java/org/springframework/cloud/consul/binder/config/ConsulBinderConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2016 the original author or authors. + * Copyright 2013-2017 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. @@ -16,15 +16,12 @@ package org.springframework.cloud.consul.binder.config; -/** - */ - -import com.ecwid.consul.v1.ConsulClient; -import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.consul.ConditionalOnConsulEnabled; import org.springframework.cloud.consul.binder.ConsulBinder; import org.springframework.cloud.consul.binder.EventService; import org.springframework.cloud.stream.binder.Binder; @@ -32,6 +29,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import com.ecwid.consul.v1.ConsulClient; +import com.fasterxml.jackson.databind.ObjectMapper; + /** * Configures the Consul binder. * @@ -40,6 +40,8 @@ import org.springframework.context.annotation.Import; @Configuration @ConditionalOnMissingBean(Binder.class) @Import({ PropertyPlaceholderAutoConfiguration.class }) +@ConditionalOnConsulEnabled +@ConditionalOnProperty(name = "spring.cloud.consul.binder.enabled", matchIfMissing = true) @EnableConfigurationProperties({ConsulBinderProperties.class}) public class ConsulBinderConfiguration { diff --git a/spring-cloud-consul-binder/src/test/java/org/springframework/cloud/consul/binder/config/ConsulBinderConfigurationTests.java b/spring-cloud-consul-binder/src/test/java/org/springframework/cloud/consul/binder/config/ConsulBinderConfigurationTests.java new file mode 100644 index 00000000..1856b8ab --- /dev/null +++ b/spring-cloud-consul-binder/src/test/java/org/springframework/cloud/consul/binder/config/ConsulBinderConfigurationTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2013-2017 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.consul.binder.config; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.stream.annotation.EnableBinding; +import org.springframework.cloud.stream.annotation.Output; +import org.springframework.context.annotation.Configuration; +import org.springframework.messaging.MessageChannel; + +import static org.hamcrest.Matchers.containsString; + +/** + * @author Spencer Gibb + */ +public class ConsulBinderConfigurationTests { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void consulBinderDisabledWorks() { + this.exception.expectMessage(containsString("no proper implementation found")); + new SpringApplicationBuilder(Application.class) + .properties("spring.cloud.consul.binder.enabled=false") + .run(); + } + + @Test + public void consulDisabledDisablesBinder() { + this.exception.expectMessage(containsString("no proper implementation found")); + new SpringApplicationBuilder(Application.class) + .properties("spring.cloud.consul.enabled=false") + .run(); + } + + interface Events { + @Output + MessageChannel purchases(); + } + + @Configuration + @EnableAutoConfiguration + @EnableBinding(Events.class) + public static class Application { + } +}