Skip to content

Commit 6f575fc

Browse files
committed
Clarify docs about component scanning of command classes
1 parent 9dbdd71 commit 6f575fc

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

spring-shell-docs/modules/ROOT/pages/commands/registration.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ There are two different ways to define a command, through an annotation model an
99
- In the annotation model, you define your methods in a `@Component` class and the methods with specific annotations.
1010
- In the programmatic model, you use a more low level approach, defining commands as beans.
1111

12+
IMPORTANT: With the annotation model, the class containing command definitions should *not* be excluded from component scanning, otherwise the commands will not be registered. This is a common mistake when using `@ComponentScan` with `excludeFilters` and excluding the package containing command classes.
13+
1214
[[commands-registration-annotation]]
1315
== Annotation-Based Registration
1416

spring-shell-docs/src/test/java/org/springframework/shell/docs/CommandAnnotationSnippets.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 the original author or authors.
2+
* Copyright 2023-present 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.
@@ -19,20 +19,22 @@
1919
import org.springframework.shell.core.command.annotation.Option;
2020
import org.springframework.shell.core.command.annotation.Argument;
2121
import org.springframework.shell.core.command.annotation.EnableCommand;
22+
import org.springframework.stereotype.Component;
2223

2324
class CommandAnnotationSnippets {
2425

2526
class Dump1 {
2627

2728
// tag::enablecommand-with-class[]
28-
@EnableCommand({ Example1.class, Example2.class })
29+
@EnableCommand({ MyCommands.class })
2930
class App {
3031

3132
}
3233
// end::enablecommand-with-class[]
3334

3435
// tag::command-anno-in-method[]
35-
class Example1 {
36+
@Component
37+
class MyCommands {
3638

3739
@Command(name = "example")
3840
public String example() {
@@ -43,7 +45,8 @@ public String example() {
4345
// end::command-anno-in-method[]
4446

4547
// tag::command-option-arg[]
46-
class Example2 {
48+
@Component
49+
class GreetingCommands {
4750

4851
@Command(name = "hi", description = "Say hi to a given name", group = "greetings",
4952
help = "A command that greets the user with 'Hi ${name}!' with a configurable suffix. Example usage: hi -s=! John")

0 commit comments

Comments
 (0)