Skip to content

Commit dbb9bf9

Browse files
committed
Revise contribution
See gh-35491
1 parent fe04bfc commit dbb9bf9

File tree

2 files changed

+42
-34
lines changed

2 files changed

+42
-34
lines changed

framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,29 @@ sure that they are 'opened' (that is, that they use an `opens` declaration inste
323323
`exports` declaration in your `module-info` descriptor).
324324
====
325325

326-
==== Property placeholders and Ant-style patterns
326+
Furthermore, the `AutowiredAnnotationBeanPostProcessor` and
327+
`CommonAnnotationBeanPostProcessor` are both implicitly included when you use the
328+
`<context:component-scan>` element. That means that the two components are autodetected
329+
and wired together -- all without any bean configuration metadata provided in XML.
330+
331+
NOTE: You can disable the registration of `AutowiredAnnotationBeanPostProcessor` and
332+
`CommonAnnotationBeanPostProcessor` by including the `annotation-config` attribute
333+
with a value of `false`.
334+
335+
336+
[[beans-scanning-placeholders-and-patterns]]
337+
=== Property Placeholders and Ant-style Patterns
338+
339+
The `basePackages` and `value` attributes in `@ComponentScan` support `${...}` property
340+
placeholders which are resolved against the `Environment` as well as Ant-style package
341+
patterns such as `"org.example.+++**+++"`.
342+
343+
In addition, multiple packages or patterns may be specified, either separately or within
344+
a single String — for example, `{"org.example.config", "org.example.service.+++**+++"}`
345+
or `"org.example.config, org.example.service.+++**+++"`.
327346

328-
`@ComponentScan(basePackages)` supports `${…}` property placeholders resolved
329-
against the `Environment` and Ant-style package patterns such as `com.example.**`.
330-
Multiple packages and/or patterns may be specified.
347+
The following example specifies the `app.scan.packages` property placeholder for the
348+
implicit `value` attribute in `@ComponentScan`.
331349

332350
[tabs]
333351
======
@@ -336,41 +354,36 @@ Java::
336354
[source,java,indent=0,subs="verbatim,quotes"]
337355
----
338356
@Configuration
339-
@ComponentScan(basePackages = "${app.scan.packages}")
357+
@ComponentScan("${app.scan.packages}") // <1>
340358
public class AppConfig {
341359
// ...
342360
}
343361
----
362+
<1> `app.scan.packages` property placeholder to be resolved against the `Environment`
344363
345364
Kotlin::
346365
+
347366
[source,kotlin,indent=0,subs="verbatim,quotes"]
348367
----
349368
@Configuration
350-
@ComponentScan(basePackages = ["\${app.scan.packages}"])
369+
@ComponentScan(["\${app.scan.packages}"]) // <1>
351370
class AppConfig {
352371
// ...
353372
}
354373
----
374+
<1> `app.scan.packages` property placeholder to be resolved against the `Environment`
355375
======
356376

377+
The following listing represents a properties file which defines the `app.scan.packages`
378+
property. In the preceding example, it is assumed that this properties file has been
379+
registered with the `Environment` – for example, via `@PropertySource` or a similar
380+
mechanism.
381+
357382
[source,properties,indent=0,subs="verbatim,quotes"]
358383
----
359-
app.scan.packages=com.example.**,org.acme.*
384+
app.scan.packages=org.example.config, org.example.service.**
360385
----
361386

362-
NOTE: Ant-style patterns do not apply to `basePackageClasses`, which accepts concrete
363-
classes and derives packages from those classes.
364-
365-
Furthermore, the `AutowiredAnnotationBeanPostProcessor` and
366-
`CommonAnnotationBeanPostProcessor` are both implicitly included when you use the
367-
component-scan element. That means that the two components are autodetected and
368-
wired together -- all without any bean configuration metadata provided in XML.
369-
370-
NOTE: You can disable the registration of `AutowiredAnnotationBeanPostProcessor` and
371-
`CommonAnnotationBeanPostProcessor` by including the `annotation-config` attribute
372-
with a value of `false`.
373-
374387

375388
[[beans-scanning-filters]]
376389
== Using Filters to Customize Scanning

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,8 @@
7474
/**
7575
* Alias for {@link #basePackages}.
7676
* <p>Allows for more concise annotation declarations if no other attributes
77-
* are needed &mdash; for example, {@code @ComponentScan("org.my.pkg")}
78-
* instead of {@code @ComponentScan(basePackages = "org.my.pkg")}.
79-
* <p>This attribute has the same semantics as {@link #basePackages}, including
80-
* support for {@code ${...}} placeholders (resolved against the
81-
* {@link org.springframework.core.env.Environment Environment}) and
82-
* Ant-style package patterns (for example, {@code com.example.**}).
77+
* are needed &mdash; for example, {@code @ComponentScan("org.example")}
78+
* instead of {@code @ComponentScan(basePackages = "org.example")}.
8379
*/
8480
@AliasFor("basePackages")
8581
String[] value() default {};
@@ -88,15 +84,16 @@
8884
* Base packages to scan for annotated components.
8985
* <p>{@link #value} is an alias for (and mutually exclusive with) this
9086
* attribute.
87+
* <p>Supports {@code ${...}} placeholders which are resolved against the
88+
* {@link org.springframework.core.env.Environment Environment} as well as
89+
* Ant-style package patterns &mdash; for example, {@code "org.example.**"}.
90+
* <p>Multiple packages or patterns may be specified, either separately or
91+
* within a single {@code String} &mdash; for example,
92+
* {@code {"org.example.config", "org.example.service.**"}} or
93+
* {@code "org.example.config, org.example.service.**"}.
9194
* <p>Use {@link #basePackageClasses} for a type-safe alternative to
9295
* String-based package names.
93-
* <p>Supports {@code ${...}} placeholders resolved against the
94-
* {@link org.springframework.core.env.Environment Environment} as well as
95-
* Ant-style package patterns (for example, {@code com.example.**}).
96-
* Multiple packages and/or patterns may be specified.
97-
* <p><strong>Note:</strong> Ant-style patterns are <em>not</em> applicable to
98-
* {@link #basePackageClasses()}, which accepts concrete classes for type-safe
99-
* package selection.
96+
* @see org.springframework.context.ConfigurableApplicationContext#CONFIG_LOCATION_DELIMITERS
10097
*/
10198
@AliasFor("value")
10299
String[] basePackages() default {};
@@ -106,8 +103,6 @@
106103
* to scan for annotated components. The package of each class specified will be scanned.
107104
* <p>Consider creating a special no-op marker class or interface in each package
108105
* that serves no purpose other than being referenced by this attribute.
109-
* <p><strong>Note:</strong> Ant-style package patterns do not apply here; this
110-
* attribute accepts concrete classes only and derives packages from those classes.
111106
*/
112107
Class<?>[] basePackageClasses() default {};
113108

0 commit comments

Comments
 (0)