I have Spring Boot application with spring-boot-starter-remote-shell. When I put this hello.groovy script it prints 'hello' and that's OK.
package commands
import org.crsh.cli.Usage
import org.crsh.cli.Command
class hello {
    @Usage("Say Hello")
    @Command
    def main(InvocationContext context) {
        return "hello";
    }
}
But when I try to inject some Spring bean it's always null.
package commands
import org.crsh.cli.Usage
import org.crsh.cli.Command
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.batch.core.launch.JobLauncher
@Component
class hello {
    @Autowired
    JobLauncher jobLauncher;
    @Usage("Say Hello")
    @Command
    def main(InvocationContext context) {
        if(jobLauncher != null){
            return "OK";
        }else{
            return "NULL";
        }
        return "hello j";
    }
}
I have @ComponentScan(basePackages={"com....", "commands"})
Spring BeanFactory can be taken from the Invocation context.
package commands
import org.crsh.cli.Usage
import org.crsh.cli.Command
import org.crsh.command.InvocationContext;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.batch.core.launch.JobLauncher
class hello {
    @Usage("Say Hello")
    @Command
    def main(InvocationContext context) {
        BeanFactory beanFactory = (BeanFactory) context.getAttributes().get("spring.beanfactory");
        JobLauncher jobLauncher = beanFactory.getBean(JobLauncher.class);
        if(jobLauncher != null){
            return jobLauncher.toString();
        }else{
            return "NULL";
        }
        return "hello j";
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With