From 0ba24af395822359889dd4d04e414016ed8876a3 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 10 Jul 2015 08:44:58 +0100 Subject: [PATCH] Re-instate search=CURRENT for config properties In order to run a module as a child context (namespaceing the channels) you need to have a separate ChannelBindingProperties per context, so the condition has to have search=CURRENT. For an XD module this was a problem because the ModuleProperties (a subclass) is needed and that has to be added in bootstrap (i.e. in the parent context). Fixed by registering the ModuleProperties as a singleton in the child context. --- .../ChannelBindingAdapterConfiguration.java | 16 +------- ...nelBindingPropertiesAutoConfiguration.java | 37 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + ...annelBindingAdapterConfigurationTests.java | 2 +- ...oduleOptionsPropertySourceInitializer.java | 4 +- 5 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 spring-cloud-streams/src/main/java/org/springframework/cloud/streams/config/ChannelBindingPropertiesAutoConfiguration.java diff --git a/spring-cloud-streams/src/main/java/org/springframework/cloud/streams/config/ChannelBindingAdapterConfiguration.java b/spring-cloud-streams/src/main/java/org/springframework/cloud/streams/config/ChannelBindingAdapterConfiguration.java index 1b862664a..621125900 100644 --- a/spring-cloud-streams/src/main/java/org/springframework/cloud/streams/config/ChannelBindingAdapterConfiguration.java +++ b/spring-cloud-streams/src/main/java/org/springframework/cloud/streams/config/ChannelBindingAdapterConfiguration.java @@ -25,7 +25,6 @@ import java.util.Set; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; - import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.target.LazyInitTargetSource; import org.springframework.beans.factory.BeanFactoryUtils; @@ -57,7 +56,6 @@ import org.springframework.xd.dirt.integration.bus.serializer.kryo.PojoCodec; * @author David Turanski */ @Configuration - public class ChannelBindingAdapterConfiguration { @Autowired @@ -172,24 +170,14 @@ public class ChannelBindingAdapterConfiguration { } @Configuration - //TODO: make sure to set the SearchStrategy to CURRENT once the module options initializer code is removed. - @ConditionalOnMissingBean(value=ChannelBindingProperties.class) - protected static class ModulePropertiesConfiguration { - @Bean(name = "spring.cloud.channels.CONFIGURATION_PROPERTIES") - public ChannelBindingProperties moduleProperties() { - return new ChannelBindingProperties(); - } - } - - protected static class CodecConfiguration { @Autowired ApplicationContext applicationContext; @Bean @ConditionalOnMissingBean(name = "codec") - public MultiTypeCodec codec() { - Map kryoRegistrarMap = applicationContext.getBeansOfType(KryoRegistrar + public MultiTypeCodec codec() { + Map kryoRegistrarMap = this.applicationContext.getBeansOfType(KryoRegistrar .class); return new PojoCodec(new ArrayList<>(kryoRegistrarMap.values())); } diff --git a/spring-cloud-streams/src/main/java/org/springframework/cloud/streams/config/ChannelBindingPropertiesAutoConfiguration.java b/spring-cloud-streams/src/main/java/org/springframework/cloud/streams/config/ChannelBindingPropertiesAutoConfiguration.java new file mode 100644 index 000000000..65f790270 --- /dev/null +++ b/spring-cloud-streams/src/main/java/org/springframework/cloud/streams/config/ChannelBindingPropertiesAutoConfiguration.java @@ -0,0 +1,37 @@ +/* + * Copyright 2015 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.streams.config; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.SearchStrategy; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author Dave Syer + * + */ +@Configuration +@ConditionalOnMissingBean(value = ChannelBindingProperties.class, search = SearchStrategy.CURRENT) +public class ChannelBindingPropertiesAutoConfiguration { + + @Bean(name = "spring.cloud.channels.CONFIGURATION_PROPERTIES") + public ChannelBindingProperties moduleProperties() { + return new ChannelBindingProperties(); + } + +} diff --git a/spring-cloud-streams/src/main/resources/META-INF/spring.factories b/spring-cloud-streams/src/main/resources/META-INF/spring.factories index 335d864cd..cbae45173 100644 --- a/spring-cloud-streams/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-streams/src/main/resources/META-INF/spring.factories @@ -1 +1,2 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration:\ +org.springframework.cloud.streams.config.ChannelBindingPropertiesAutoConfiguration \ No newline at end of file diff --git a/spring-cloud-streams/src/test/java/org/springframework/cloud/streams/config/ChannelBindingAdapterConfigurationTests.java b/spring-cloud-streams/src/test/java/org/springframework/cloud/streams/config/ChannelBindingAdapterConfigurationTests.java index e59c1e228..98ccf6972 100644 --- a/spring-cloud-streams/src/test/java/org/springframework/cloud/streams/config/ChannelBindingAdapterConfigurationTests.java +++ b/spring-cloud-streams/src/test/java/org/springframework/cloud/streams/config/ChannelBindingAdapterConfigurationTests.java @@ -139,7 +139,7 @@ public class ChannelBindingAdapterConfigurationTests { } @Configuration - @Import(ChannelBindingAdapterConfiguration.class) + @Import({ChannelBindingAdapterConfiguration.class, ChannelBindingPropertiesAutoConfiguration.class}) protected static class Empty { @Bean public LocalMessageBus messageBus() { diff --git a/spring-xd-runner/src/main/java/org/springframework/cloud/streams/xd/ModuleOptionsPropertySourceInitializer.java b/spring-xd-runner/src/main/java/org/springframework/cloud/streams/xd/ModuleOptionsPropertySourceInitializer.java index 6b67eb23d..1135863ce 100644 --- a/spring-xd-runner/src/main/java/org/springframework/cloud/streams/xd/ModuleOptionsPropertySourceInitializer.java +++ b/spring-xd-runner/src/main/java/org/springframework/cloud/streams/xd/ModuleOptionsPropertySourceInitializer.java @@ -79,6 +79,8 @@ ApplicationContextInitializer { } } insert(environment, new MapPropertySource("moduleDefaults", map)); + applicationContext.getBeanFactory().registerSingleton( + "spring.cloud.channels.CONFIGURATION_PROPERTIES", this.module); } private ModuleDefinition getModuleDefinition( @@ -131,7 +133,7 @@ ApplicationContextInitializer { } @Configuration - @ConditionalOnMissingBean(value=ModuleProperties.class, search=SearchStrategy.CURRENT) + @ConditionalOnMissingBean(value = ModuleProperties.class, search = SearchStrategy.CURRENT) protected static class ModulePropertiesConfiguration { @Bean(name = "spring.cloud.channels.CONFIGURATION_PROPERTIES") public ModuleProperties moduleProperties() {