简体   繁体   中英

spring boot remote shell custom command

I try to add a new custom command to the spring boot remote shell without success. In the documentation is only a groovy example available but I like to use Java do create a new command.

http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-remote-shell.html I also check the CRaSH documentation: http://www.crashub.org/1.3/reference.html#_java_commands

I put my class under package commands and crash.commands but I don't see my new command if I connect to the shell via ssh and type help. Any idea what I'm doing wrong?

Here is my Java Code:

package commands;

import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.command.BaseCommand;
import org.springframework.stereotype.Component;

@Component
public class hello extends BaseCommand {

  @Command
  @Usage("Say Hello")
  public String test() {
    return "HELLO";
  }
} 

If your class is called 'hello', put it in

resources/crash/commands/hello/hello.java

Having a package statement doesn't matter. You don't have to put anything inside src, just have this inside resources.

@Component isn't required.

This class is compiled during runtime. See:

org.crsh.lang.impl.java.JavaCompiler#compileCommand

Therefor any dependency injection requirements need to be considered accordingly with lazy initialization.

Put the class in the default package (ie omit the package statement) and put the Java source for the class in src/main/resources/commands. Spring/Crash will compile the Java code the first time you invoke the command. Also, if the class implements a single command the method name should be 'main' otherwise users have to type:

> hello test

You also don't need the @Component annotation on the class as it is not a Spring managed bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM