Skip to content

Commit fe04bfc

Browse files
Rockernunsbrannen
authored andcommitted
Document placeholder and pattern support for @⁠ComponentScan
- JavaDoc: clarify that basePackages/value resolve ${…} via Environment and accept Ant-style package patterns (e.g., com.example.**); note patterns don’t apply to basePackageClasses. - Reference: add “Property placeholders and Ant-style patterns” subsection in classpath-scanning.adoc with Java/Kotlin + properties examples. See gh-35288 Closes gh-35491 Signed-off-by: Byeong-Uk Park <[email protected]>
1 parent 015edb3 commit fe04bfc

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,45 @@ 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
327+
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.
331+
332+
[tabs]
333+
======
334+
Java::
335+
+
336+
[source,java,indent=0,subs="verbatim,quotes"]
337+
----
338+
@Configuration
339+
@ComponentScan(basePackages = "${app.scan.packages}")
340+
public class AppConfig {
341+
// ...
342+
}
343+
----
344+
345+
Kotlin::
346+
+
347+
[source,kotlin,indent=0,subs="verbatim,quotes"]
348+
----
349+
@Configuration
350+
@ComponentScan(basePackages = ["\${app.scan.packages}"])
351+
class AppConfig {
352+
// ...
353+
}
354+
----
355+
======
356+
357+
[source,properties,indent=0,subs="verbatim,quotes"]
358+
----
359+
app.scan.packages=com.example.**,org.acme.*
360+
----
361+
362+
NOTE: Ant-style patterns do not apply to `basePackageClasses`, which accepts concrete
363+
classes and derives packages from those classes.
364+
326365
Furthermore, the `AutowiredAnnotationBeanPostProcessor` and
327366
`CommonAnnotationBeanPostProcessor` are both implicitly included when you use the
328367
component-scan element. That means that the two components are autodetected and

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
* <p>Allows for more concise annotation declarations if no other attributes
7777
* are needed &mdash; for example, {@code @ComponentScan("org.my.pkg")}
7878
* 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.**}).
7983
*/
8084
@AliasFor("basePackages")
8185
String[] value() default {};
@@ -86,6 +90,13 @@
8690
* attribute.
8791
* <p>Use {@link #basePackageClasses} for a type-safe alternative to
8892
* 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.
89100
*/
90101
@AliasFor("value")
91102
String[] basePackages() default {};
@@ -95,6 +106,8 @@
95106
* to scan for annotated components. The package of each class specified will be scanned.
96107
* <p>Consider creating a special no-op marker class or interface in each package
97108
* 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.
98111
*/
99112
Class<?>[] basePackageClasses() default {};
100113

0 commit comments

Comments
 (0)