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 6657938c3a..376a331d27 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 @@ -18,6 +18,8 @@ package org.springframework.integration.config; import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -48,6 +50,7 @@ import org.springframework.core.type.filter.AspectJTypeFilter; import org.springframework.core.type.filter.AssignableTypeFilter; import org.springframework.core.type.filter.RegexPatternTypeFilter; import org.springframework.core.type.filter.TypeFilter; +import org.springframework.integration.annotation.IntegrationComponentScan; import org.springframework.integration.annotation.MessagingGateway; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -86,26 +89,13 @@ public class IntegrationComponentScanRegistrar implements ImportBeanDefinitionRe @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - Map componentScan = importingClassMetadata - .getAnnotationAttributes("org.springframework.integration.annotation.IntegrationComponentScan"); + Map componentScan = + importingClassMetadata.getAnnotationAttributes(IntegrationComponentScan.class.getName()); - Set basePackages = new HashSet(); - for (String pkg : (String[]) componentScan.get("value")) { - if (StringUtils.hasText(pkg)) { - basePackages.add(pkg); - } - } - for (String pkg : (String[]) componentScan.get("basePackages")) { - if (StringUtils.hasText(pkg)) { - basePackages.add(pkg); - } - } - for (Class clazz : (Class[]) componentScan.get("basePackageClasses")) { - basePackages.add(ClassUtils.getPackageName(clazz)); - } + Collection basePackages = getBasePackages(importingClassMetadata, registry); if (basePackages.isEmpty()) { - basePackages.add(ClassUtils.getPackageName(importingClassMetadata.getClassName())); + basePackages = Collections.singleton(ClassUtils.getPackageName(importingClassMetadata.getClassName())); } ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false) { @@ -150,6 +140,26 @@ public class IntegrationComponentScanRegistrar implements ImportBeanDefinitionRe } } + protected Collection getBasePackages(AnnotationMetadata importingClassMetadata, + BeanDefinitionRegistry registry) { + Map componentScan = + importingClassMetadata.getAnnotationAttributes(IntegrationComponentScan.class.getName()); + + Set basePackages = new HashSet<>(); + + for (String pkg : (String[]) componentScan.get("value")) { + if (StringUtils.hasText(pkg)) { + basePackages.add(pkg); + } + } + + for (Class clazz : (Class[]) componentScan.get("basePackageClasses")) { + basePackages.add(ClassUtils.getPackageName(clazz)); + } + + return basePackages; + } + private List typeFiltersFor(AnnotationAttributes filter, BeanDefinitionRegistry registry) { List typeFilters = new ArrayList<>(); FilterType filterType = filter.getEnum("type"); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dsl/flows/IntegrationFlowTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dsl/flows/IntegrationFlowTests.java index 121f140cb9..e4bf6e663b 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dsl/flows/IntegrationFlowTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dsl/flows/IntegrationFlowTests.java @@ -419,7 +419,7 @@ public class IntegrationFlowTests { @MessagingGateway(defaultRequestChannel = "controlBus") - private interface ControlBusGateway { + public interface ControlBusGateway { void send(String command); } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/enablecomponentscan/EnableComponentScanTests.java b/spring-integration-core/src/test/java/org/springframework/integration/enablecomponentscan/EnableComponentScanTests.java new file mode 100644 index 0000000000..37f4964bac --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/enablecomponentscan/EnableComponentScanTests.java @@ -0,0 +1,91 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.enablecomponentscan; + +import static org.hamcrest.Matchers.emptyArray; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; + +import java.util.Collection; +import java.util.Collections; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.core.type.StandardAnnotationMetadata; +import org.springframework.integration.annotation.IntegrationComponentScan; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.config.IntegrationComponentScanRegistrar; +import org.springframework.integration.dsl.flows.IntegrationFlowTests; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.ClassUtils; + +/** + * @author Artem Bilan + * @author Gary Russell + * @since 4.0 + */ +@RunWith(SpringRunner.class) +@DirtiesContext +public class EnableComponentScanTests { + + @Autowired + private ApplicationContext applicationContext; + + @Test + public void testCustomIntegrationComponentScan() { + assertThat(applicationContext.getBeanNamesForType(IntegrationFlowTests.ControlBusGateway.class), + not(emptyArray())); + } + + @Configuration + @EnableIntegration + @Import(CustomIntegrationComponentScanRegistrar.class) + public static class IntegrationComponentScanAutoConfiguration { + + } + + private static class CustomIntegrationComponentScanRegistrar extends IntegrationComponentScanRegistrar { + + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, + BeanDefinitionRegistry registry) { + super.registerBeanDefinitions(new StandardAnnotationMetadata( + IntegrationComponentScanConfiguration.class, true), registry); + } + + @Override + protected Collection getBasePackages(AnnotationMetadata importingClassMetadata, + BeanDefinitionRegistry registry) { + return Collections.singleton(ClassUtils.getPackageName(IntegrationFlowTests.ControlBusGateway.class)); + } + + @IntegrationComponentScan + private static class IntegrationComponentScanConfiguration { + + } + + } + +}