Saturday, 30 January 2021

Spring Shell: Customize command names

By default, name of the method is used as command key.

@ShellComponent(value="Commands to perform Arithmetic Operations")
public class ArithmeticCommands {

        @ShellMethod("Add two integers together.")
        public int add(int a, int b) {
                return a + b;
        }

}

In the above example, ‘add’ is used as command name. If you want to customize it to name ‘sum’, you can specify it using the key attribute of @ShellMethod annotation.

@ShellComponent(value = "Commands to perform Arithmetic Operations by customizing key name")
public class ArithmeticCommandCustomizeCommandName {
    
    @ShellMethod(value = "Add two integers together.", key="sum")
    public int add(int a, int b) {
        return a + b;
    }
}

 

With this change, you can execute the addition of two numbers using the command ‘sum’.

shell:>sum 10 20
30
shell:>sum 123 234
357
shell:>

 

You can download complete application from this link.

https://github.com/harikrishna553/springboot/tree/master/shell/hello-world

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment