Skip to content

Commit 2dca5b6

Browse files
philwebbPhillip Webb
authored andcommitted
Filter source @configuration with @componentscan
Exclude self @component when parsing @componentscan annotations. Issue: SPR-10531 (cherry picked from d91ffb)
1 parent a302c4a commit 2dca5b6

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ComponentScanAnnotationParser.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.core.annotation.AnnotationAttributes;
2929
import org.springframework.core.env.Environment;
3030
import org.springframework.core.io.ResourceLoader;
31+
import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter;
3132
import org.springframework.core.type.filter.AnnotationTypeFilter;
3233
import org.springframework.core.type.filter.AssignableTypeFilter;
3334
import org.springframework.core.type.filter.TypeFilter;
@@ -64,7 +65,7 @@ public ComponentScanAnnotationParser(ResourceLoader resourceLoader, Environment
6465
}
6566

6667

67-
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, String declaringClass) {
68+
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
6869
ClassPathBeanDefinitionScanner scanner =
6970
new ClassPathBeanDefinitionScanner(this.registry, componentScan.getBoolean("useDefaultFilters"));
7071

@@ -120,6 +121,12 @@ public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, Strin
120121
basePackages.add(ClassUtils.getPackageName(declaringClass));
121122
}
122123

124+
scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
125+
@Override
126+
protected boolean matchClassName(String className) {
127+
return declaringClass.equals(className);
128+
}
129+
});
123130
return scanner.doScan(StringUtils.toStringArray(basePackages));
124131
}
125132

0 commit comments

Comments
 (0)