Spring Boot 2.1 - further updates

Update more tests with bean overriding errors where they try to override the `BinderTypeRegistry` bean.
This is not very common in end user applications unless they are trying to define a new binder type.
If that is indeed the case, then they have to set `spring.main.allow-bean-definition-overriding` to `true`.

Remove where we define the bean `integrationArgumentResolverMessageConverter` in `ContentTypeConfiguration`
as this causes a conflict with the same bean from Spring Integration and thus causing bean overriding exception.
Insted of creating this through `@Bean`, manually register the bean after removing it from `BeanDefinitionRegistry`.
This commit is contained in:
Soby Chacko
2018-08-07 17:17:22 -04:00
parent cb105ab0ec
commit 85b34be733
22 changed files with 107 additions and 32 deletions

View File

@@ -22,10 +22,14 @@ import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.cloud.stream.annotation.StreamMessageConverter;
import org.springframework.cloud.stream.converter.CompositeMessageConverterFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;
@@ -39,11 +43,14 @@ import org.springframework.util.CollectionUtils;
*/
@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class ContentTypeConfiguration {
public class ContentTypeConfiguration implements InitializingBean {
@Autowired(required = false)
private ObjectMapper objectMapper;
@Autowired
private ConfigurableApplicationContext context;
/**
* User defined custom message converters
*/
@@ -51,17 +58,42 @@ public class ContentTypeConfiguration {
@StreamMessageConverter
private List<MessageConverter> customMessageConverters;
@Override
public void afterPropertiesSet() throws Exception {
if (context.getBeanFactory().containsBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME)) {
BeanDefinitionRegistry beanDefinitionRegistry =
(BeanDefinitionRegistry) context.getAutowireCapableBeanFactory();
beanDefinitionRegistry.removeBeanDefinition(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME);
}
}
@Bean
@SuppressWarnings("unchecked")
public CompositeMessageConverterFactory compositeMessageConverterFactory() {
List<MessageConverter> messageConverters = new ArrayList<>();
if (!CollectionUtils.isEmpty(this.customMessageConverters)) {
messageConverters.addAll(Collections.unmodifiableCollection(this.customMessageConverters));
}
return new CompositeMessageConverterFactory(messageConverters, this.objectMapper);
CompositeMessageConverterFactory compositeMessageConverterFactory = new CompositeMessageConverterFactory(messageConverters, this.objectMapper);
// Manually register a bean named as `integrationArgumentResolverMessageConverter`
// in order to avoid bean name overriding exceptions. This name exists in Spring Integration.
// The afterProperties method should have removed the bean from the registry and then
// we are re-registering it again through a different bean definition.
BeanDefinitionRegistry beanDefinitionRegistry =
(BeanDefinitionRegistry) context.getAutowireCapableBeanFactory();
ConfigurableCompositeMessageConverter configurableCompositeMessageConverter =
new ConfigurableCompositeMessageConverter(compositeMessageConverterFactory.getMessageConverterForAllRegistered().getConverters());
BeanDefinition configurableCompositeMessageConverterDefn =
BeanDefinitionBuilder.genericBeanDefinition((Class<ConfigurableCompositeMessageConverter>) configurableCompositeMessageConverter.getClass(),
() -> configurableCompositeMessageConverter)
.getRawBeanDefinition();
beanDefinitionRegistry.registerBeanDefinition(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME, configurableCompositeMessageConverterDefn);
return compositeMessageConverterFactory;
}
@Bean(name = IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME)
public ConfigurableCompositeMessageConverter configurableCompositeMessageConverter(CompositeMessageConverterFactory factory){
return new ConfigurableCompositeMessageConverter(factory.getMessageConverterForAllRegistered().getConverters());
}
}

View File

@@ -40,7 +40,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Janne Valkealahti
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ArbitraryInterfaceWithBindingTargetsTests.TestFooChannels.class)
@SpringBootTest(classes = ArbitraryInterfaceWithBindingTargetsTests.TestFooChannels.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class ArbitraryInterfaceWithBindingTargetsTests {
@Autowired

View File

@@ -39,7 +39,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Janne Valkealahti
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ArbitraryInterfaceWithDefaultsTests.TestFooChannels.class)
@SpringBootTest(classes = ArbitraryInterfaceWithDefaultsTests.TestFooChannels.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class ArbitraryInterfaceWithDefaultsTests {
@Autowired

View File

@@ -94,7 +94,7 @@ public class BinderAwareChannelResolverTests {
@SuppressWarnings("unchecked")
@Before
public void setupContext() throws Exception {
//System.setProperty("spring.main.allow-bean-definition-overriding", "true");
System.setProperty("spring.main.allow-bean-definition-overriding", "true");
this.context = new SpringApplicationBuilder(TestChannelBinderConfiguration.getCompleteConfiguration(BinderAwareChannelResolverTests.InterceptorConfiguration.class))
.web(WebApplicationType.NONE).run();

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.stream.binder;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
@@ -37,6 +38,11 @@ import static org.mockito.ArgumentMatchers.isNull;
*/
public class ErrorBindingTests {
@Before
public void before() {
System.setProperty("spring.main.allow-bean-definition-overriding", "true");
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void testErrorChannelNotBoundByDefault() {

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.stream.binder;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
@@ -45,6 +46,11 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*/
public class InputOutputBindingOrderTest {
@Before
public void before() {
System.setProperty("spring.main.allow-bean-definition-overriding", "true");
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void testInputOutputBindingOrder() {

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.stream.binder;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
@@ -36,6 +37,11 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class LifecycleBinderTests {
@Before
public void before() {
System.setProperty("spring.main.allow-bean-definition-overriding", "true");
}
@Test
public void testOnlySmartLifecyclesStarted() {
ConfigurableApplicationContext applicationContext = SpringApplication.run(TestSource.class, "--server.port=-1");

View File

@@ -63,6 +63,7 @@ public class PollableConsumerTests {
@Before
public void before() {
//System.setProperty("spring.main.allow-bean-definition-overriding", "true");
this.messageConverter = new CompositeMessageConverterFactory().getMessageConverterForAllRegistered();
}

View File

@@ -40,7 +40,8 @@ import static org.mockito.Mockito.verify;
* @author Janne Valkealahti
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ProcessorBindingWithBindingTargetsTests.TestProcessor.class)
@SpringBootTest(classes = ProcessorBindingWithBindingTargetsTests.TestProcessor.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class ProcessorBindingWithBindingTargetsTests {
@Autowired

View File

@@ -39,7 +39,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Janne Valkealahti
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ProcessorBindingsWithDefaultsTests.TestProcessor.class)
@SpringBootTest(classes = ProcessorBindingsWithDefaultsTests.TestProcessor.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class ProcessorBindingsWithDefaultsTests {
@Autowired

View File

@@ -42,7 +42,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Janne Valkealahti
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SinkBindingWithDefaultTargetsTests.TestSink.class)
@SpringBootTest(classes = SinkBindingWithDefaultTargetsTests.TestSink.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class SinkBindingWithDefaultTargetsTests {
@Autowired

View File

@@ -40,7 +40,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Janne Valkealahti
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SinkBindingWithDefaultsTests.TestSink.class)
@SpringBootTest(classes = SinkBindingWithDefaultsTests.TestSink.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class SinkBindingWithDefaultsTests {
@Autowired

View File

@@ -43,7 +43,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Ilayaperumal Gopinathan
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SourceBindingWithBindingTargetsTests.TestSource.class)
@SpringBootTest(classes = SourceBindingWithBindingTargetsTests.TestSource.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class SourceBindingWithBindingTargetsTests {
@Autowired

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.stream.binder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
@@ -38,7 +39,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Marius Bogoevici
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SourceBindingWithDefaultsTests.TestSource.class)
@SpringBootTest(classes = SourceBindingWithDefaultsTests.TestSource.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class SourceBindingWithDefaultsTests {
@Autowired
@@ -47,6 +49,11 @@ public class SourceBindingWithDefaultsTests {
@Autowired
private Source testSource;
@Before
public void before() {
System.setProperty("spring.main.allow-bean-definition-overriding", "true");
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Test
public void testSourceOutputChannelBound() {

View File

@@ -38,7 +38,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {TestChannelBinderConfiguration.class, SourceBindingWithGlobalPropertiesOnlyTest.TestSource.class, SpelExpressionConverterConfiguration.class},
properties = {"spring.cloud.stream.default.contentType=application/json",
"spring.cloud.stream.default.producer.partitionKeyExpression=key" })
"spring.cloud.stream.default.producer.partitionKeyExpression=key"})
public class SourceBindingWithGlobalPropertiesOnlyTest {
@Autowired

View File

@@ -44,7 +44,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
"spring.cloud.stream.default.producer.requiredGroups=someGroup",
"spring.cloud.stream.default.producer.partitionCount=1",
"spring.cloud.stream.bindings.output.producer.headerMode=none",
"spring.cloud.stream.bindings.output.producer.partitionCount=4"})
"spring.cloud.stream.bindings.output.producer.partitionCount=4",
"spring.main.allow-bean-definition-overriding=true"})
public class SourceBindingWithGlobalPropertiesTest {
@Autowired

View File

@@ -23,6 +23,7 @@ import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.WebApplicationType;
@@ -73,6 +74,11 @@ import static org.junit.Assert.assertTrue;
*/
public class ContentTypeTckTests {
@Before
public void before() {
System.setProperty("spring.main.allow-bean-definition-overriding", "true");
}
@Test
public void stringToMapStreamListener() {
ApplicationContext context = new SpringApplicationBuilder(StringToMapStreamListener.class)

View File

@@ -56,7 +56,8 @@ public class CustomPartitionedProducerTest {
"--spring.jmx.enabled=false",
"--spring.main.web-application-type=none",
"--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorClass=org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass",
"--spring.cloud.stream.bindings.output.producer.partitionSelectorClass=org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass");
"--spring.cloud.stream.bindings.output.producer.partitionSelectorClass=org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass",
"--spring.main.allow-bean-definition-overriding=true");
Source testSource = context.getBean(Source.class);
DirectChannel messageChannel = (DirectChannel) testSource.output();
for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
@@ -87,7 +88,8 @@ public class CustomPartitionedProducerTest {
"--spring.jmx.enabled=false",
"--spring.main.web-application-type=none",
"--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractor",
"--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelector");
"--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelector",
"--spring.main.allow-bean-definition-overriding=true");
Source testSource = context.getBean(Source.class);
DirectChannel messageChannel = (DirectChannel) testSource.output();
for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
@@ -115,7 +117,7 @@ public class CustomPartitionedProducerTest {
@Test
public void testCustomPartitionedProducerAsSingletons() {
ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class,
"--spring.jmx.enabled=false", "--spring.main.web-application-type=none");
"--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.main.allow-bean-definition-overriding=true");
Source testSource = context.getBean(Source.class);
DirectChannel messageChannel = (DirectChannel) testSource.output();
for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {

View File

@@ -16,7 +16,7 @@
package org.springframework.cloud.stream.binding;
import org.assertj.core.api.ThrowableAssert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
@@ -39,17 +39,15 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
*/
public class InvalidBindingConfigurationTests {
@Before
public void before() {
System.setProperty("spring.main.allow-bean-definition-overriding", "true");
}
@Test
public void testDuplicateBeanByBindingConfig() {
assertThatThrownBy(
new ThrowableAssert.ThrowingCallable() {
@Override
public void call() throws Throwable {
SpringApplication.run(TestBindingConfig.class);
}
})
() -> SpringApplication.run(TestBindingConfig.class))
.isInstanceOf(BeanDefinitionStoreException.class)
.hasMessageContaining("bean definition with this name already exists")
.hasMessageContaining(TestInvalidBinding.NAME)

View File

@@ -53,7 +53,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Artem Bilan
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpelExpressionConverterConfigurationTests.Config.class, properties = "expression: a.b")
@SpringBootTest(classes = SpelExpressionConverterConfigurationTests.Config.class,
properties = {"expression: a.b", "spring.main.allow-bean-definition-overriding=true"})
public class SpelExpressionConverterConfigurationTests {
@Autowired

View File

@@ -48,7 +48,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Oleg Zhurakousky
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BoundChannelsInterceptedTest.Foo.class)
@SpringBootTest(classes = BoundChannelsInterceptedTest.Foo.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class BoundChannelsInterceptedTest {
public static final Message<?> TEST_MESSAGE = MessageBuilder.withPayload("bar").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON).build();

View File

@@ -49,7 +49,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Janne Valkealahti
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = PartitionedConsumerTest.TestSink.class)
@SpringBootTest(classes = PartitionedConsumerTest.TestSink.class,
properties = "spring.main.allow-bean-definition-overriding=true")
public class PartitionedConsumerTest {
@Autowired