Skip to content

Commit 07e08b3

Browse files
committed
Document registration with @command
- Split and separate new annotations with legacy annotations on a registration level. - Relates #637
1 parent 1170ec9 commit 07e08b3

5 files changed

+131
-3
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[[commands-registration-annotation]]
2+
==== Annotation
3+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
4+
5+
`@Command` annotation when used on a method marks it as a candidate for command registration.
6+
In below example a command `example` is defined.
7+
8+
====
9+
[source, java, indent=0]
10+
----
11+
include::{snippets}/CommandAnnotationSnippets.java[tag=command-anno-in-method]
12+
----
13+
====
14+
15+
`@Command` annotation can be placed on a class which either defines defaults or shared settings
16+
for `@Command` methods defined in a same class. In below example a command `parent example` is
17+
defined.
18+
19+
====
20+
[source, java, indent=0]
21+
----
22+
include::{snippets}/CommandAnnotationSnippets.java[tag=command-anno-in-class]
23+
----
24+
====
25+
26+
Using a `@Command` will not automatically register command targets, instead it is required to use
27+
`@EnableCommand` and/or `@CommandScan` annotations. This model is familiar from other parts
28+
of Spring umbrella and provides better flexibility for a user being inclusive rather than exclusive
29+
for command targets.
30+
31+
You can define target classes using `@EnableCommand`. It will get picked from all _Configuration_
32+
classes.
33+
34+
====
35+
[source, java, indent=0]
36+
----
37+
include::{snippets}/CommandAnnotationSnippets.java[tag=enablecommand-with-class]
38+
----
39+
====
40+
41+
You can define target classes using `@CommandScan`. It will get picked from all _Configuration_
42+
classes.
43+
44+
TIP: Define `@CommandScan` in Spring Boot `App` class on a top level and it will automatically
45+
scan all command targets from all packages and classes under `App`.
46+
47+
====
48+
[source, java, indent=0]
49+
----
50+
include::{snippets}/CommandAnnotationSnippets.java[tag=commandscan-no-args]
51+
----
52+
====

spring-shell-docs/src/main/asciidoc/using-shell-commands-registration-legacyannotation.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
==== Annotation Model
1+
[[commands-registration-legacyannotation]]
2+
==== Annotation Legacy
23
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
34

45
When you use the standard API, methods on beans are turned into executable commands, provided that:

spring-shell-docs/src/main/asciidoc/using-shell-commands-registration-programmatic.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
[[using-shell-commands-programmaticmodel]]
2-
==== Programmatic Model
1+
[[commands-registration-programmatic]]
2+
==== Programmatic
33
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
44

55
In the programmatic model, `CommandRegistration` can be defined as a `@Bean`

spring-shell-docs/src/main/asciidoc/using-shell-commands-registration.adoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ in a class and annotate the class and the methods with specific annotations.
77
In the programmatic model, you use a more low level approach, defining command
88
registrations (either as beans or by dynamically registering with a command catalog).
99

10+
Starting from _3.1.x_ a better support for defining commands using
11+
<<commands-registration-annotation, annotations>> were added. Firstly because eventually standard
12+
package providing <<commands-registration-legacyannotation, legacy annotations>> will get deprecated
13+
and removed. Secondly so that we're able to provide same set of features than using underlying
14+
`CommandRegistration`. Creating new a annotation model allows us to rethink and modernise that
15+
part without breaking existing applications.
16+
17+
include::using-shell-commands-registration-annotation.adoc[]
18+
1019
include::using-shell-commands-registration-legacyannotation.adoc[]
1120

1221
include::using-shell-commands-registration-programmatic.adoc[]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.docs;
17+
18+
import org.springframework.shell.command.annotation.Command;
19+
import org.springframework.shell.command.annotation.CommandScan;
20+
import org.springframework.shell.command.annotation.EnableCommand;
21+
22+
class CommandAnnotationSnippets {
23+
24+
class Dump1 {
25+
26+
// tag::enablecommand-with-class[]
27+
@EnableCommand(Example.class)
28+
class App {
29+
}
30+
// end::enablecommand-with-class[]
31+
32+
// tag::command-anno-in-method[]
33+
class Example {
34+
35+
@Command(command = "example")
36+
public String example() {
37+
return "Hello";
38+
}
39+
}
40+
// end::command-anno-in-method[]
41+
}
42+
43+
class Dump2 {
44+
45+
// tag::command-anno-in-class[]
46+
@Command(command = "parent")
47+
class Example {
48+
49+
@Command(command = "example")
50+
public String example() {
51+
return "Hello";
52+
}
53+
}
54+
// end::command-anno-in-class[]
55+
}
56+
57+
class Dump3 {
58+
59+
// tag::commandscan-no-args[]
60+
@CommandScan
61+
class App {
62+
}
63+
// end::commandscan-no-args[]
64+
}
65+
66+
}

0 commit comments

Comments
 (0)