diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindingService.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindingService.java index 4ebf24d02..2544d14b8 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindingService.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binding/BindingService.java @@ -29,6 +29,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeanUtils; +import org.springframework.beans.BeansException; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver; import org.springframework.boot.context.properties.source.ConfigurationPropertySources; @@ -44,6 +45,8 @@ import org.springframework.cloud.stream.binder.PollableSource; import org.springframework.cloud.stream.binder.ProducerProperties; import org.springframework.cloud.stream.config.BindingServiceProperties; import org.springframework.cloud.stream.config.MergableProperties; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.integration.support.utils.IntegrationUtils; import org.springframework.scheduling.TaskScheduler; @@ -64,7 +67,7 @@ import org.springframework.validation.beanvalidation.CustomValidatorBean; * @author Janne Valkealahti * @author Soby Chacko */ -public class BindingService { +public class BindingService implements ApplicationContextAware { private final CustomValidatorBean validator; @@ -80,24 +83,27 @@ public class BindingService { private final BinderFactory binderFactory; - private final ConfigurableApplicationContext applicationContext; + private ConfigurableApplicationContext applicationContext; public BindingService( BindingServiceProperties bindingServiceProperties, BinderFactory binderFactory) { - this(bindingServiceProperties, binderFactory, null, null); + this(bindingServiceProperties, binderFactory, null); } public BindingService( BindingServiceProperties bindingServiceProperties, - BinderFactory binderFactory, TaskScheduler taskScheduler, - ConfigurableApplicationContext applicationContext) { + BinderFactory binderFactory, TaskScheduler taskScheduler) { this.bindingServiceProperties = bindingServiceProperties; this.binderFactory = binderFactory; this.validator = new CustomValidatorBean(); this.validator.afterPropertiesSet(); this.taskScheduler = taskScheduler; - this.applicationContext = applicationContext; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = (ConfigurableApplicationContext) applicationContext; } @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -251,7 +257,7 @@ public class BindingService { return binding; } - private void handleExtendedDefaultProperties(ExtendedPropertiesBinder binder, MergableProperties extendedProperties, String filedName) { + private void handleExtendedDefaultProperties(ExtendedPropertiesBinder binder, MergableProperties extendedProperties, String filedName) { String defaultsPrefix = binder.getDefaultsPrefix(); Class extendedPropertiesEntryClass = binder.getExtendedPropertiesEntryClass(); diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceConfiguration.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceConfiguration.java index c16ac0f93..beff86d0f 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceConfiguration.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/BindingServiceConfiguration.java @@ -47,7 +47,6 @@ import org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPost import org.springframework.cloud.stream.function.StreamFunctionProperties; import org.springframework.cloud.stream.micrometer.DestinationPublishingMetricsAutoConfiguration; import org.springframework.context.ApplicationListener; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; @@ -164,10 +163,9 @@ public class BindingServiceConfiguration { // already exists). @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) public BindingService bindingService(BindingServiceProperties bindingServiceProperties, - BinderFactory binderFactory, TaskScheduler taskScheduler, - ConfigurableApplicationContext applicationContext) { + BinderFactory binderFactory, TaskScheduler taskScheduler) { - return new BindingService(bindingServiceProperties, binderFactory, taskScheduler, applicationContext); + return new BindingService(bindingServiceProperties, binderFactory, taskScheduler); } @Bean diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/MergableProperties.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/MergableProperties.java index b3b671f34..2b88ca7d0 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/MergableProperties.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/MergableProperties.java @@ -35,6 +35,7 @@ import org.springframework.util.ObjectUtils; * * @author Oleg Zhurakousky * @author Soby Chacko + * * @see BinderProperties * @see ProducerProperties * @see ConsumerProperties @@ -103,19 +104,19 @@ public interface MergableProperties { } default boolean isEmptyMapAtDestination(Object v) { - return Map.class.isAssignableFrom(v.getClass()) && CollectionUtils.isEmpty((Map) v); + return Map.class.isAssignableFrom(v.getClass()) && CollectionUtils.isEmpty((Map) v); } default boolean isMergableByMap(Object v) { - return (Map.class.isAssignableFrom(v.getClass()) && !CollectionUtils.isEmpty((Map) v)); + return (Map.class.isAssignableFrom(v.getClass()) && !CollectionUtils.isEmpty((Map) v)); } @SuppressWarnings("unchecked") default void handleMapMerging(Object value, Object v) { if (value instanceof Map) { - Map sourceMap = (Map) value; + Map sourceMap = (Map) value; for (Object key : sourceMap.keySet()) { - Map targetMap = (Map) v; + Map targetMap = (Map) v; if (!targetMap.containsKey(key)) { targetMap.put(key, sourceMap.get(key)); } @@ -124,6 +125,6 @@ public interface MergableProperties { } default void copyProperties(Object source, Object target) throws BeansException { - + // noop } } diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binding/BindingServiceTests.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binding/BindingServiceTests.java index 0c4eda585..89e756de4 100644 --- a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binding/BindingServiceTests.java +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/binding/BindingServiceTests.java @@ -379,7 +379,8 @@ public class BindingServiceTests { environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd)); applicationContext.setEnvironment(environment); - BindingService service = new BindingService(serviceProperties, binderFactory, null, applicationContext); + BindingService service = new BindingService(serviceProperties, binderFactory, null); + service.setApplicationContext(applicationContext); MessageChannel outputChannel = new DirectChannel(); Binder binder = binderFactory.getBinder(null, MessageChannel.class); @@ -413,7 +414,8 @@ public class BindingServiceTests { environment.getPropertySources().addLast(new MapPropertySource("extPropertiesConfig", propertiesToAdd)); applicationContext.setEnvironment(environment); - BindingService service = new BindingService(serviceProperties, binderFactory, null, applicationContext); + BindingService service = new BindingService(serviceProperties, binderFactory, null); + service.setApplicationContext(applicationContext); MessageChannel inputChannel = new DirectChannel(); Binder binder = binderFactory.getBinder(null, MessageChannel.class); @@ -533,7 +535,7 @@ public class BindingServiceTests { Binder binder = binderFactory.getBinder("mock", MessageChannel.class); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.initialize(); - BindingService service = new BindingService(properties, binderFactory, scheduler, null); + BindingService service = new BindingService(properties, binderFactory, scheduler); MessageChannel inputChannel = new DirectChannel(); final Binding mockBinding = Mockito.mock(Binding.class); final CountDownLatch fail = new CountDownLatch(2); @@ -576,7 +578,7 @@ public class BindingServiceTests { Binder binder = binderFactory.getBinder("mock", MessageChannel.class); ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.initialize(); - BindingService service = new BindingService(properties, binderFactory, scheduler, null); + BindingService service = new BindingService(properties, binderFactory, scheduler); MessageChannel outputChannel = new DirectChannel(); final Binding mockBinding = Mockito.mock(Binding.class); final CountDownLatch fail = new CountDownLatch(2); diff --git a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/MockExtendedBinderConfiguration.java b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/MockExtendedBinderConfiguration.java index a41d65626..8767f06c4 100644 --- a/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/MockExtendedBinderConfiguration.java +++ b/spring-cloud-stream/src/test/java/org/springframework/cloud/stream/utils/MockExtendedBinderConfiguration.java @@ -31,6 +31,7 @@ import static org.mockito.Mockito.when; @Configuration public class MockExtendedBinderConfiguration { + @SuppressWarnings("rawtypes") @Bean public Binder extendedPropertiesBinder() { Binder mock = Mockito.mock(Binder.class, Mockito.withSettings().defaultAnswer(Mockito.RETURNS_MOCKS)