Skip to content

Commit 5e47a6a

Browse files
artembilangaryrussell
authored andcommitted
INT-4517: Refactor beans in IntegrationRegistrar (#2537)
* INT-4517: Refactor beans in IntegrationRegistrar JIRA: https://jira.spring.io/browse/INT-4517 The `IntegrationRegistrar` populates beans into the application context too early and when the `allowBeanDefinitionsOverride` is disabled we end up with the `BeanDefinitionOverrideException` just because end-user beans are applied later. * Move most of the bean definitions which can be overridden in the target application from the `IntegrationRegistrar` into the `DefaultConfiguringBeanFactoryPostProcessor` * Revise their registration logic to be sure that some of them are registered only once in the parent context nad others are registered in the child context as well * Refactor a `DslIntegrationConfigurationInitializer` do not register an `IntegrationFlowDefinition.ReplyProducerCleaner` in the child context one more time: it doesn't bring any local store within the current child context * Introduce a `NoBeansOverrideAnnotationConfigContextLoader` in tests to ensure that in some test-cases we disallow an `allowBeanDefinitionsOverride` as it is now in Spring Boot since it is enabled by default in the Spring Framework * Revernt DslIntegrationConfigurationInitializer changes Regsiter all the required infrastructure beans in all the application contexts to avoid class loader issue for `static` store access
1 parent 60db7bc commit 5e47a6a

File tree

11 files changed

+499
-512
lines changed

11 files changed

+499
-512
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java

Lines changed: 404 additions & 77 deletions
Large diffs are not rendered by default.

spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationConfigUtils.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,16 +20,13 @@
2020
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2121
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
2222
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
23-
import org.springframework.beans.factory.support.ManagedList;
24-
import org.springframework.context.Lifecycle;
2523
import org.springframework.integration.channel.DirectChannel;
26-
import org.springframework.integration.context.IntegrationContextUtils;
27-
import org.springframework.integration.support.SmartLifecycleRoleController;
2824

2925
/**
3026
* Shared utility methods for Integration configuration.
3127
*
3228
* @author Artem Bilan
29+
*
3330
* @since 4.0
3431
*/
3532
public final class IntegrationConfigUtils {
@@ -43,6 +40,7 @@ public final class IntegrationConfigUtils {
4340

4441
public static void registerSpelFunctionBean(BeanDefinitionRegistry registry, String functionId, String className,
4542
String methodSignature) {
43+
4644
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SpelFunctionFactoryBean.class)
4745
.addConstructorArgValue(className)
4846
.addConstructorArgValue(methodSignature);
@@ -55,17 +53,6 @@ public static void autoCreateDirectChannel(String channelName, BeanDefinitionReg
5553
BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry);
5654
}
5755

58-
public static void registerRoleControllerDefinitionIfNecessary(BeanDefinitionRegistry registry) {
59-
if (!registry.containsBeanDefinition(
60-
IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER)) {
61-
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SmartLifecycleRoleController.class);
62-
builder.addConstructorArgValue(new ManagedList<String>());
63-
builder.addConstructorArgValue(new ManagedList<Lifecycle>());
64-
registry.registerBeanDefinition(
65-
IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER, builder.getBeanDefinition());
66-
}
67-
}
68-
6956
private IntegrationConfigUtils() {
7057
}
7158

spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationRegistrar.java

Lines changed: 13 additions & 386 deletions
Large diffs are not rendered by default.

spring-integration-core/src/main/java/org/springframework/integration/config/xml/AbstractIntegrationNamespaceHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public final BeanDefinition parse(Element element, ParserContext parserContext)
5757
if (!this.initialized.getAndSet(true)) {
5858
verifySchemaVersion(element, parserContext);
5959
IntegrationRegistrar integrationRegistrar = new IntegrationRegistrar();
60-
integrationRegistrar.setBeanClassLoader(parserContext.getReaderContext().getBeanClassLoader());
6160
integrationRegistrar.registerBeanDefinitions(null, parserContext.getRegistry());
6261
}
6362
return this.delegate.parse(element, parserContext);

spring-integration-core/src/main/java/org/springframework/integration/config/xml/AnnotationConfigParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class AnnotationConfigParser implements BeanDefinitionParser {
4141
@Override
4242
public BeanDefinition parse(final Element element, ParserContext parserContext) {
4343
IntegrationRegistrar integrationRegistrar = new IntegrationRegistrar();
44-
integrationRegistrar.setBeanClassLoader(parserContext.getReaderContext().getBeanClassLoader());
4544

4645
StandardAnnotationMetadata importingClassMetadata =
4746
new StandardAnnotationMetadata(Object.class) {

spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationContextUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public abstract class IntegrationContextUtils {
4141

4242
public static final String ERROR_CHANNEL_BEAN_NAME = "errorChannel";
4343

44+
public static final String ERROR_LOGGER_BEAN_NAME = "_org.springframework.integration.errorLogger";
45+
4446
public static final String NULL_CHANNEL_BEAN_NAME = "nullChannel";
4547

4648
public static final String METADATA_STORE_BEAN_NAME = "metadataStore";

spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
import org.springframework.integration.support.SmartLifecycleRoleController;
119119
import org.springframework.integration.test.util.OnlyOnceTrigger;
120120
import org.springframework.integration.test.util.TestUtils;
121+
import org.springframework.integration.util.NoBeansOverrideAnnotationConfigContextLoader;
121122
import org.springframework.messaging.Message;
122123
import org.springframework.messaging.MessageChannel;
123124
import org.springframework.messaging.MessageDeliveryException;
@@ -136,7 +137,6 @@
136137
import org.springframework.test.annotation.DirtiesContext;
137138
import org.springframework.test.context.ContextConfiguration;
138139
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
139-
import org.springframework.test.context.support.AnnotationConfigContextLoader;
140140
import org.springframework.util.ClassUtils;
141141
import org.springframework.util.MultiValueMap;
142142

@@ -148,7 +148,7 @@
148148
* @author Gary Russell
149149
* @since 4.0
150150
*/
151-
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
151+
@ContextConfiguration(loader = NoBeansOverrideAnnotationConfigContextLoader.class,
152152
classes = { EnableIntegrationTests.ContextConfiguration.class,
153153
EnableIntegrationTests.ContextConfiguration2.class })
154154
@RunWith(SpringJUnit4ClassRunner.class)
@@ -961,6 +961,7 @@ public Number convert(Boolean source) {
961961
public Integer getInvoked() {
962962
return invoked.get();
963963
}
964+
964965
}
965966

966967
@Configuration
@@ -1443,6 +1444,7 @@ public interface TestGateway2 {
14431444
String phase() default "";
14441445

14451446
Poller[] poller() default { };
1447+
14461448
}
14471449

14481450
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@@ -1461,6 +1463,7 @@ public interface TestGateway2 {
14611463
String phase() default "";
14621464

14631465
Poller[] poller() default { };
1466+
14641467
}
14651468

14661469
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@@ -1566,6 +1569,7 @@ public interface TestGateway2 {
15661569
String phase() default "";
15671570

15681571
Poller[] poller() default { };
1572+
15691573
}
15701574

15711575
@Target(ElementType.METHOD)
@@ -1614,6 +1618,7 @@ public interface TestGateway2 {
16141618
public @interface MyBridgeFrom {
16151619

16161620
String value() default "";
1621+
16171622
}
16181623

16191624
@Target(ElementType.METHOD)

spring-integration-core/src/test/java/org/springframework/integration/dsl/flows/IntegrationFlowTests.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import org.springframework.integration.support.MessageBuilder;
7676
import org.springframework.integration.support.MutableMessageBuilder;
7777
import org.springframework.integration.transformer.PayloadSerializingTransformer;
78+
import org.springframework.integration.util.NoBeansOverrideAnnotationConfigContextLoader;
7879
import org.springframework.messaging.Message;
7980
import org.springframework.messaging.MessageChannel;
8081
import org.springframework.messaging.MessageDeliveryException;
@@ -91,6 +92,7 @@
9192
import org.springframework.stereotype.Component;
9293
import org.springframework.stereotype.Service;
9394
import org.springframework.test.annotation.DirtiesContext;
95+
import org.springframework.test.context.ContextConfiguration;
9496
import org.springframework.test.context.junit4.SpringRunner;
9597

9698
/**
@@ -101,6 +103,7 @@
101103
*
102104
* @since 5.0
103105
*/
106+
@ContextConfiguration(loader = NoBeansOverrideAnnotationConfigContextLoader.class)
104107
@RunWith(SpringRunner.class)
105108
@DirtiesContext
106109
public class IntegrationFlowTests {
@@ -482,11 +485,13 @@ public void testDedicatedPollingThreadFlow() throws InterruptedException {
482485
public interface ControlBusGateway {
483486

484487
void send(String command);
488+
485489
}
486490

487491
@Configuration
488492
@EnableIntegration
489493
public static class SupplierContextConfiguration1 {
494+
490495
@Bean
491496
public IntegrationFlow supplierFlow() {
492497
return IntegrationFlows.from(() -> "foo")
@@ -512,15 +517,17 @@ public TaskScheduler taskScheduler() {
512517
public MessageChannel suppliedChannel() {
513518
return MessageChannels.queue(10).get();
514519
}
520+
515521
}
516522

517523
@Configuration
518524
@EnableIntegration
519525
public static class SupplierContextConfiguration2 {
526+
520527
@Bean
521528
public IntegrationFlow supplierFlow2() {
522529
return IntegrationFlows.from(() -> "foo", c -> c.poller(Pollers.fixedDelay(100).maxMessagesPerPoll(1)))
523-
.<String, String>transform(p -> p.toUpperCase())
530+
.<String, String>transform(String::toUpperCase)
524531
.channel("suppliedChannel2")
525532
.get();
526533
}
@@ -529,6 +536,7 @@ public IntegrationFlow supplierFlow2() {
529536
public MessageChannel suppliedChannel2() {
530537
return MessageChannels.queue(10).get();
531538
}
539+
532540
}
533541

534542
@Configuration
@@ -542,18 +550,6 @@ public IntegrationFlow controlBusFlow() {
542550
.get();
543551
}
544552

545-
@Bean(name = PollerMetadata.DEFAULT_POLLER)
546-
public PollerMetadata poller() {
547-
return Pollers.fixedRate(500).get();
548-
}
549-
550-
@Bean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
551-
public TaskScheduler taskScheduler() {
552-
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
553-
threadPoolTaskScheduler.setPoolSize(100);
554-
return threadPoolTaskScheduler;
555-
}
556-
557553
@Bean
558554
public MessageChannel inputChannel() {
559555
return MessageChannels.direct().get();
@@ -690,6 +686,7 @@ public static class AnnotationTestService {
690686
public void handle(Object payload) {
691687
assertEquals(100, payload);
692688
}
689+
693690
}
694691

695692
@Configuration
@@ -856,6 +853,7 @@ public static class GreetingService extends AbstractReplyProducingMessageHandler
856853
protected Object handleRequestMessage(Message<?> requestMessage) {
857854
return "Hello " + this.worldService.world() + " and " + requestMessage.getPayload();
858855
}
856+
859857
}
860858

861859
@Service
@@ -864,6 +862,7 @@ public static class WorldService {
864862
public String world() {
865863
return "World";
866864
}
865+
867866
}
868867

869868

spring-integration-core/src/test/java/org/springframework/integration/dsl/manualflow/ManualFlowTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import org.springframework.integration.support.SmartLifecycleRoleController;
8383
import org.springframework.integration.test.util.TestUtils;
8484
import org.springframework.integration.transformer.MessageTransformingHandler;
85+
import org.springframework.integration.util.NoBeansOverrideAnnotationConfigContextLoader;
8586
import org.springframework.messaging.Message;
8687
import org.springframework.messaging.MessageChannel;
8788
import org.springframework.messaging.MessageDeliveryException;
@@ -102,7 +103,8 @@
102103
*
103104
* @since 5.0
104105
*/
105-
@ContextConfiguration(classes = ManualFlowTests.RootConfiguration.class)
106+
@ContextConfiguration(loader = NoBeansOverrideAnnotationConfigContextLoader.class,
107+
classes = ManualFlowTests.RootConfiguration.class)
106108
@RunWith(SpringRunner.class)
107109
@DirtiesContext
108110
public class ManualFlowTests {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.integration.util;
18+
19+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
20+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
21+
22+
/**
23+
* @author Artem Bilan
24+
*
25+
* @since 5.1
26+
*/
27+
public class NoBeansOverrideAnnotationConfigContextLoader extends AnnotationConfigContextLoader {
28+
29+
@Override
30+
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
31+
beanFactory.setAllowBeanDefinitionOverriding(false);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)