Skip to content

Commit e0d77a3

Browse files
artembilangaryrussell
authored andcommitted
GH-3374: Fix scan for BF propagation (#3378)
* GH-3374: Fix scan for BF propagation Fixes #3374 An internal `ClassPathScanningCandidateComponentProvider` instance in the `IntegrationComponentScanRegistrar` does not propagate a provided `registry`. * Implement `getRegistry()` on the internal `ClassPathScanningCandidateComponentProvider` to propagate a provided into the `registerBeanDefinitions()` a `BeanDefinitionRegistry` * Add `@Conditional` on some scanned `@MessagingGateway` in the `EnableIntegrationTests` **Cherry-pick to 5.3.x & 5.2.x** * * Remove unused import * Restore `unused` warning on the unused registry arg
1 parent 7fbd24e commit e0d77a3

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@
6767
public class IntegrationComponentScanRegistrar implements ImportBeanDefinitionRegistrar,
6868
ResourceLoaderAware, EnvironmentAware {
6969

70-
private final Map<TypeFilter, ImportBeanDefinitionRegistrar> componentRegistrars =
71-
new HashMap<TypeFilter, ImportBeanDefinitionRegistrar>();
70+
private final Map<TypeFilter, ImportBeanDefinitionRegistrar> componentRegistrars = new HashMap<>();
7271

7372
private ResourceLoader resourceLoader;
7473

@@ -109,6 +108,11 @@ protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
109108
&& !beanDefinition.getMetadata().isAnnotation();
110109
}
111110

111+
@Override
112+
protected BeanDefinitionRegistry getRegistry() {
113+
return registry;
114+
}
115+
112116
};
113117

114118
filter(registry, componentScan, scanner); // NOSONAR - never null
@@ -163,7 +167,7 @@ protected Collection<String> getBasePackages(AnnotationMetadata importingClassMe
163167
}
164168
}
165169

166-
for (Class<?> clazz : (Class[]) componentScan.get("basePackageClasses")) {
170+
for (Class<?> clazz : (Class<?>[]) componentScan.get("basePackageClasses")) {
167171
basePackages.add(ClassUtils.getPackageName(clazz));
168172
}
169173

@@ -216,10 +220,13 @@ private List<TypeFilter> typeFiltersFor(AnnotationAttributes filter, BeanDefinit
216220

217221
private static void invokeAwareMethods(Object parserStrategyBean, Environment environment,
218222
ResourceLoader resourceLoader, BeanDefinitionRegistry registry) {
223+
219224
if (parserStrategyBean instanceof Aware) {
220225
if (parserStrategyBean instanceof BeanClassLoaderAware) {
221-
ClassLoader classLoader = (registry instanceof ConfigurableBeanFactory ?
222-
((ConfigurableBeanFactory) registry).getBeanClassLoader() : resourceLoader.getClassLoader());
226+
ClassLoader classLoader =
227+
registry instanceof ConfigurableBeanFactory
228+
? ((ConfigurableBeanFactory) registry).getBeanClassLoader()
229+
: resourceLoader.getClassLoader();
223230
if (classLoader != null) {
224231
((BeanClassLoaderAware) parserStrategyBean).setBeanClassLoader(classLoader);
225232
}

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

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.springframework.integration.configuration;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
21+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2122
import static org.mockito.Mockito.doAnswer;
2223
import static org.mockito.Mockito.mock;
2324
import static org.mockito.Mockito.spy;
@@ -43,6 +44,7 @@
4344

4445
import org.springframework.beans.DirectFieldAccessor;
4546
import org.springframework.beans.factory.FactoryBean;
47+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
4648
import org.springframework.beans.factory.annotation.Autowired;
4749
import org.springframework.beans.factory.annotation.Qualifier;
4850
import org.springframework.beans.factory.config.AbstractFactoryBean;
@@ -53,12 +55,16 @@
5355
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5456
import org.springframework.context.annotation.Bean;
5557
import org.springframework.context.annotation.ComponentScan;
58+
import org.springframework.context.annotation.Condition;
59+
import org.springframework.context.annotation.ConditionContext;
60+
import org.springframework.context.annotation.Conditional;
5661
import org.springframework.context.annotation.Configuration;
5762
import org.springframework.context.annotation.ImportResource;
5863
import org.springframework.context.expression.EnvironmentAccessor;
5964
import org.springframework.context.expression.MapAccessor;
6065
import org.springframework.core.convert.converter.Converter;
6166
import org.springframework.core.serializer.support.SerializingConverter;
67+
import org.springframework.core.type.AnnotatedTypeMetadata;
6268
import org.springframework.expression.EvaluationContext;
6369
import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
6470
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -432,15 +438,11 @@ public void testAnnotatedServiceActivator() throws Exception {
432438
@Test
433439
@DirtiesContext
434440
public void testChangePatterns() {
435-
try {
436-
this.configurer.setComponentNamePatterns(new String[] { "*" });
437-
fail("ExpectedException");
438-
}
439-
catch (IllegalStateException e) {
440-
assertThat(e.getMessage()).contains("cannot be changed");
441-
}
441+
assertThatIllegalStateException()
442+
.isThrownBy(() -> this.configurer.setComponentNamePatterns(new String[]{ "*" }))
443+
.withMessageContaining("cannot be changed");
442444
this.configurer.stop();
443-
this.configurer.setComponentNamePatterns(new String[] { "*" });
445+
this.configurer.setComponentNamePatterns(new String[]{ "*" });
444446
assertThat(TestUtils.getPropertyValue(this.configurer, "componentNamePatterns", String[].class)[0])
445447
.isEqualTo("*");
446448
}
@@ -468,6 +470,9 @@ public void testMessagingGateway() throws InterruptedException {
468470
this.testGateway.sendAsync("foo");
469471
assertThat(this.asyncAnnotationProcessLatch.await(1, TimeUnit.SECONDS)).isTrue();
470472
assertThat(this.asyncAnnotationProcessThread.get()).isNotSameAs(Thread.currentThread());
473+
474+
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
475+
.isThrownBy(() -> this.context.getBean(ConditionalGateway.class));
471476
}
472477

473478
/**
@@ -620,14 +625,10 @@ public void testBridgeAnnotations() {
620625
assertThat(testMessage).isSameAs(receive);
621626
assertThat(this.pollableBridgeOutput.receive(10)).isNull();
622627

623-
try {
624-
this.metaBridgeInput.send(testMessage);
625-
fail("MessageDeliveryException expected");
626-
}
627-
catch (Exception e) {
628-
assertThat(e).isInstanceOf(MessageDeliveryException.class);
629-
assertThat(e.getMessage()).contains("Dispatcher has no subscribers");
630-
}
628+
629+
assertThatExceptionOfType(MessageDeliveryException.class)
630+
.isThrownBy(() -> this.metaBridgeInput.send(testMessage))
631+
.withMessageContaining("Dispatcher has no subscribers");
631632

632633
this.context.getBean("enableIntegrationTests.ContextConfiguration.metaBridgeOutput.bridgeFrom",
633634
Lifecycle.class).start();
@@ -652,14 +653,9 @@ public void testBridgeAnnotations() {
652653
assertThat(bridgeMessage).isSameAs(receive);
653654
assertThat(replyChannel.receive(10)).isNull();
654655

655-
try {
656-
this.myBridgeToInput.send(testMessage);
657-
fail("MessageDeliveryException expected");
658-
}
659-
catch (Exception e) {
660-
assertThat(e).isInstanceOf(MessageDeliveryException.class);
661-
assertThat(e.getMessage()).contains("Dispatcher has no subscribers");
662-
}
656+
assertThatExceptionOfType(MessageDeliveryException.class)
657+
.isThrownBy(() -> this.myBridgeToInput.send(testMessage))
658+
.withMessageContaining("Dispatcher has no subscribers");
663659

664660
this.context.getBean("enableIntegrationTests.ContextConfiguration.myBridgeToInput.bridgeTo",
665661
Lifecycle.class).start();
@@ -762,7 +758,7 @@ public void testIntegrationEvaluationContextCustomization() {
762758
private PollableChannel myHandlerSuccessChannel;
763759

764760
@Test
765-
public void testAdvicedServiceActivator() {
761+
public void testAdvisedServiceActivator() {
766762
Date testDate = new Date();
767763

768764
this.myHandlerChannel.send(new GenericMessage<>(testDate));
@@ -1411,6 +1407,14 @@ public boolean isRunning() {
14111407

14121408
}
14131409

1410+
@Conditional(TestCondition.class)
1411+
@MessagingGateway
1412+
public interface ConditionalGateway {
1413+
1414+
void testGateway(Object payload);
1415+
1416+
}
1417+
14141418
@TestMessagingGateway
14151419
public interface TestGateway {
14161420

@@ -1673,4 +1677,13 @@ public static Object bar(Object o) {
16731677

16741678
}
16751679

1680+
public static class TestCondition implements Condition {
1681+
1682+
@Override
1683+
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
1684+
return context.getBeanFactory().containsBean("DoesNotExist");
1685+
}
1686+
1687+
}
1688+
16761689
}

0 commit comments

Comments
 (0)