Skip to content

Commit b109f14

Browse files
committed
Improve documentation for annotation filters with component scanning
Prior to this commit the documentation for annotation-based include and exclude filters used with component scanning did not explicitly mention the fact that annotations are considered a match if they are either present or meta-present on candidate classes. This commit improves the documentation in this regard. See gh-22551
1 parent c04eaf6 commit b109f14

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

spring-context/src/main/resources/org/springframework/context/config/spring-context.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@
515515
<xsd:documentation><![CDATA[
516516
Controls the type of filtering to apply to the expression.
517517
518-
"annotation" indicates an annotation to be present at the type level in target components;
518+
"annotation" indicates an annotation to be present or meta-present at the type level in target components;
519519
"assignable" indicates a class (or interface) that the target components are assignable to (extend/implement);
520520
"aspectj" indicates an AspectJ type pattern expression to be matched by the target components;
521521
"regex" indicates a regex pattern to be matched by the target components' class names;

spring-core/src/main/java/org/springframework/core/type/filter/AnnotationTypeFilter.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -26,14 +26,20 @@
2626
import org.springframework.util.ClassUtils;
2727

2828
/**
29-
* A simple filter which matches classes with a given annotation,
29+
* A simple {@link TypeFilter} which matches classes with a given annotation,
3030
* checking inherited annotations as well.
3131
*
32-
* <p>The matching logic mirrors that of {@link java.lang.Class#isAnnotationPresent(Class)}.
32+
* <p>By default, the matching logic mirrors that of
33+
* {@link AnnotationUtils#getAnnotation(java.lang.reflect.AnnotatedElement, Class)},
34+
* supporting annotations that are <em>present</em> or <em>meta-present</em> for a
35+
* single level of meta-annotations. The search for meta-annotations my be disabled.
36+
* Similarly, the search for annotations on interfaces may optionally be enabled.
37+
* Consult the various constructors in this class for details.
3338
*
3439
* @author Mark Fisher
3540
* @author Ramnivas Laddad
3641
* @author Juergen Hoeller
42+
* @author Sam Brannen
3743
* @since 2.5
3844
*/
3945
public class AnnotationTypeFilter extends AbstractTypeHierarchyTraversingFilter {
@@ -44,20 +50,20 @@ public class AnnotationTypeFilter extends AbstractTypeHierarchyTraversingFilter
4450

4551

4652
/**
47-
* Create a new AnnotationTypeFilter for the given annotation type.
48-
* This filter will also match meta-annotations. To disable the
53+
* Create a new {@code AnnotationTypeFilter} for the given annotation type.
54+
* <p>The filter will also match meta-annotations. To disable the
4955
* meta-annotation matching, use the constructor that accepts a
50-
* '{@code considerMetaAnnotations}' argument. The filter will
51-
* not match interfaces.
56+
* '{@code considerMetaAnnotations}' argument.
57+
* <p>The filter will not match interfaces.
5258
* @param annotationType the annotation type to match
5359
*/
5460
public AnnotationTypeFilter(Class<? extends Annotation> annotationType) {
5561
this(annotationType, true, false);
5662
}
5763

5864
/**
59-
* Create a new AnnotationTypeFilter for the given annotation type.
60-
* The filter will not match interfaces.
65+
* Create a new {@code AnnotationTypeFilter} for the given annotation type.
66+
* <p>The filter will not match interfaces.
6167
* @param annotationType the annotation type to match
6268
* @param considerMetaAnnotations whether to also match on meta-annotations
6369
*/
@@ -66,7 +72,7 @@ public AnnotationTypeFilter(Class<? extends Annotation> annotationType, boolean
6672
}
6773

6874
/**
69-
* Create a new {@link AnnotationTypeFilter} for the given annotation type.
75+
* Create a new {@code AnnotationTypeFilter} for the given annotation type.
7076
* @param annotationType the annotation type to match
7177
* @param considerMetaAnnotations whether to also match on meta-annotations
7278
* @param considerInterfaces whether to also match interfaces

src/docs/asciidoc/core/core-beans.adoc

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5499,21 +5499,22 @@ component-scan element. That means that the two components are autodetected and
54995499
wired together -- all without any bean configuration metadata provided in XML.
55005500

55015501
NOTE: You can disable the registration of `AutowiredAnnotationBeanPostProcessor` and
5502-
`CommonAnnotationBeanPostProcessor` by including the annotation-config attribute
5502+
`CommonAnnotationBeanPostProcessor` by including the `annotation-config` attribute
55035503
with a value of `false`.
55045504

55055505

55065506

55075507
[[beans-scanning-filters]]
55085508
=== Using Filters to Customize Scanning
55095509

5510-
By default, classes annotated with `@Component`, `@Repository`, `@Service`,
5511-
`@Controller`, or a custom annotation that itself is annotated with `@Component` are the
5512-
only detected candidate components. However, you can modify and extend this behavior
5513-
by applying custom filters. Add them as `includeFilters` or `excludeFilters`
5514-
parameters of the `@ComponentScan` annotation (or as `include-filter` or `exclude-filter`
5515-
child elements of the `component-scan` element). Each filter element requires the `type`
5516-
and `expression` attributes. The following table describes the filtering options:
5510+
By default, classes annotated with `@Component`, `@Repository`, `@Service`, `@Controller`,
5511+
`@Configuration`, or a custom annotation that itself is annotated with `@Component` are
5512+
the only detected candidate components. However, you can modify and extend this behavior
5513+
by applying custom filters. Add them as `includeFilters` or `excludeFilters` attributes of
5514+
the `@ComponentScan` annotation (or as `<context:include-filter />` or
5515+
`<context:exclude-filter />` child elements of the `<context:component-scan>` element in
5516+
XML configuration). Each filter element requires the `type` and `expression` attributes.
5517+
The following table describes the filtering options:
55175518

55185519
[[beans-scanning-filters-tbl]]
55195520
.Filter Types
@@ -5522,7 +5523,7 @@ and `expression` attributes. The following table describes the filtering options
55225523

55235524
| annotation (default)
55245525
| `org.example.SomeAnnotation`
5525-
| An annotation to be present at the type level in target components.
5526+
| An annotation to be _present_ or _meta-present_ at the type level in target components.
55265527

55275528
| assignable
55285529
| `org.example.SomeClass`
@@ -5534,11 +5535,11 @@ and `expression` attributes. The following table describes the filtering options
55345535

55355536
| regex
55365537
| `org\.example\.Default.*`
5537-
| A regex expression to be matched by the target components class names.
5538+
| A regex expression to be matched by the target components' class names.
55385539

55395540
| custom
55405541
| `org.example.MyTypeFilter`
5541-
| A custom implementation of the `org.springframework.core.type .TypeFilter` interface.
5542+
| A custom implementation of the `org.springframework.core.type.TypeFilter` interface.
55425543
|===
55435544

55445545
The following example shows the configuration ignoring all `@Repository` annotations
@@ -5571,10 +5572,11 @@ The following listing shows the equivalent XML:
55715572
</beans>
55725573
----
55735574

5574-
NOTE: You can also disable the default filters by setting `useDefaultFilters=false` on the annotation or
5575-
by providing `use-default-filters="false"` as an attribute of the `<component-scan/>` element. This,
5576-
in effect, disables automatic detection of classes annotated with `@Component`, `@Repository`,
5577-
`@Service`, `@Controller`, or `@Configuration`.
5575+
NOTE: You can also disable the default filters by setting `useDefaultFilters=false` on the
5576+
annotation or by providing `use-default-filters="false"` as an attribute of the
5577+
`<component-scan/>` element. This effectively disables automatic detection of classes
5578+
annotated or meta-annotated with `@Component`, `@Repository`, `@Service`, `@Controller`,
5579+
`@RestController`, or `@Configuration`.
55785580

55795581

55805582

0 commit comments

Comments
 (0)