From 88d681f4428ca83ebb2b15cf5a0b92257e4e3633 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Sat, 5 Nov 2022 12:19:26 -0400 Subject: [PATCH] Register bean for IntComponentScanRegistrar To avoid duplication for scanning register a `IntegrationComponentScanRegistrar` as a bean by itself and check for its presence before scanning * Exclude this bean definition from the AOT since its logic has already passed on AOT generation and we don't need this bean at runtime any more --- ...egrationBeanRegistrationExcludeFilter.java | 4 +++- .../IntegrationComponentScanRegistrar.java | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/aot/IntegrationBeanRegistrationExcludeFilter.java b/spring-integration-core/src/main/java/org/springframework/integration/aot/IntegrationBeanRegistrationExcludeFilter.java index bc23aa60ed..7ff402251a 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/aot/IntegrationBeanRegistrationExcludeFilter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/aot/IntegrationBeanRegistrationExcludeFilter.java @@ -21,6 +21,7 @@ import java.util.Arrays; import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter; import org.springframework.beans.factory.support.RegisteredBean; import org.springframework.integration.config.DefaultConfiguringBeanFactoryPostProcessor; +import org.springframework.integration.config.IntegrationComponentScanRegistrar; import org.springframework.integration.config.IntegrationConfigurationBeanFactoryPostProcessor; /** @@ -39,7 +40,8 @@ class IntegrationBeanRegistrationExcludeFilter implements BeanRegistrationExclud public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) { Class beanClass = registeredBean.getBeanClass(); return Arrays.asList(DefaultConfiguringBeanFactoryPostProcessor.class, - IntegrationConfigurationBeanFactoryPostProcessor.class) + IntegrationConfigurationBeanFactoryPostProcessor.class, + IntegrationComponentScanRegistrar.class) .contains(beanClass); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationComponentScanRegistrar.java b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationComponentScanRegistrar.java index 1ad92b868a..ed06da015e 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationComponentScanRegistrar.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationComponentScanRegistrar.java @@ -25,13 +25,18 @@ import java.util.List; import java.util.Set; import java.util.regex.Pattern; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.Aware; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.context.EnvironmentAware; @@ -66,6 +71,10 @@ import org.springframework.util.StringUtils; public class IntegrationComponentScanRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware { + private static final Log LOGGER = LogFactory.getLog(IntegrationComponentScanRegistrar.class); + + private static final String BEAN_NAME = IntegrationComponentScanRegistrar.class.getName(); + private final List defaultFilters = new ArrayList<>(); private ResourceLoader resourceLoader; @@ -94,6 +103,16 @@ public class IntegrationComponentScanRegistrar implements ImportBeanDefinitionRe Assert.notNull(componentScan, "The '@IntegrationComponentScan' must be present for using this registrar"); + if (registry.containsBeanDefinition(BEAN_NAME)) { + LOGGER.warn("Only one '@IntegrationComponentScan' can be present.\nConsider to merge them all to one."); + return; + } + + registry.registerBeanDefinition(BEAN_NAME, + BeanDefinitionBuilder.genericBeanDefinition(IntegrationComponentScanRegistrar.class, () -> this) + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE) + .getBeanDefinition()); + Collection basePackages = getBasePackages(componentScan, registry); if (basePackages.isEmpty()) {