Legacy Annotation

当你使用标准 API 时,bean 中的方法会作为可执行的命令,前提是:

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

  • The bean class bears the @ShellComponent annotation. (This is used to restrict the set of beans that are considered.)

  • The method bears the @ShellMethod annotation.

@ShellComponent 是一种本身元注解为 @Component 的构造型注解。因此,你除了使用筛选机制之外,也可以用它来声明 bean(例如通过使用 @ComponentScan)。

The @ShellComponent is a stereotype annotation that is itself meta-annotated with @Component. As a result, you can use it in addition to the filtering mechanism to declare beans (for example, by using @ComponentScan).

你可以使用注解的 value 属性来自定义已创建 bean 的名称。

You can customize the name of the created bean by using the value attribute of the annotation.

Unresolved include directive in modules/ROOT/pages/commands/registration/legacyannotation.adoc - include::../../test/java/org/springframework/shell/docs/AnnotationRegistrationSnippets.java[]

@ShellMethod 注解的唯一必需属性是其 value 属性,它应该对命令做什么有一个简短的、一句话的描述。这使得您的用户可以在不离开 shell 的情况下获得关于您命令的一致帮助(请参阅 Help )。

The only required attribute of the @ShellMethod annotation is its value attribute, which should have a short, one-sentence, description of what the command does. This lets your users get consistent help about your commands without having to leave the shell (see Help).

你的命令描述应简短——不超过一两句话。为达到更好的连贯性,它应以大写字母开头,以句号结尾。

The description of your command should be short — no more than one or two sentences. For better consistency, it should start with a capital letter and end with a period.

默认情况下,你不需要为你的命令指定键(也就是说,shell 中用来调用该命令的单词)。方法名称用作命令键,将驼峰式名称转换为带短划线的 GNU 风格名称(例如 sayHello() 变为 say-hello)。

By default, you need not specify the key for your command (that is, the word(s) that should be used to invoke it in the shell). The name of the method is used as the command key, turning camelCase names into dashed, gnu-style, names (for example, sayHello() becomes say-hello).

但是,你可以使用注解的 key 属性来明确设置命令键:

You can, however, explicitly set the command key, by using the key attribute of the annotation:

Unresolved include directive in modules/ROOT/pages/commands/registration/legacyannotation.adoc - include::../../test/java/org/springframework/shell/docs/AnnotationRegistrationSnippets.java[]

key 属性接受多个值。如果你为一个方法设置多个键,该命令将使用这些不同的别名注册。

The key attribute accepts multiple values. If you set multiple keys for a single method, the command is registered with those different aliases.

命令键可以包含几乎任何字符,包括空格。然而在想名称时,请记住一致性经常会受到用户青睐。即,你应避免将破折号名称与空格名称混合使用,以及其他不一致。

The command key can contain pretty much any character, including spaces. When coming up with names though, keep in mind that consistency is often appreciated by users. That is, you should avoid mixing dashed-names with spaced names and other inconsistencies.