GH-2007: Subclass for IntComponentScanRegistrar

Fixes GH-2007 (https://github.com/spring-projects/spring-integration/issues/2007)

To allow more straightforward extension and avoid some extra mocking provide some `protected` methods for the `IntegrationComponentScanRegistrar` which can be implement in some specific cases, like Spring Boot auto-configuration

The `EnableComponentScanTests` demonstrates how it can be done now in case of `basePackage` overriding
This commit is contained in:
Artem Bilan
2016-12-22 17:52:42 -05:00
committed by Gary Russell
parent a466579ee1
commit 08424fb4a5
3 changed files with 119 additions and 18 deletions

View File

@@ -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<String, Object> componentScan = importingClassMetadata
.getAnnotationAttributes("org.springframework.integration.annotation.IntegrationComponentScan");
Map<String, Object> componentScan =
importingClassMetadata.getAnnotationAttributes(IntegrationComponentScan.class.getName());
Set<String> basePackages = new HashSet<String>();
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<String> 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<String> getBasePackages(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
Map<String, Object> componentScan =
importingClassMetadata.getAnnotationAttributes(IntegrationComponentScan.class.getName());
Set<String> 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<TypeFilter> typeFiltersFor(AnnotationAttributes filter, BeanDefinitionRegistry registry) {
List<TypeFilter> typeFilters = new ArrayList<>();
FilterType filterType = filter.getEnum("type");

View File

@@ -419,7 +419,7 @@ public class IntegrationFlowTests {
@MessagingGateway(defaultRequestChannel = "controlBus")
private interface ControlBusGateway {
public interface ControlBusGateway {
void send(String command);
}

View File

@@ -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<String> getBasePackages(AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) {
return Collections.singleton(ClassUtils.getPackageName(IntegrationFlowTests.ControlBusGateway.class));
}
@IntegrationComponentScan
private static class IntegrationComponentScanConfiguration {
}
}
}