[英]Inject Spring bean in groovy bean
I have Spring Boot application with spring-boot-starter-remote-shell. 我有Spring-boot-starter-remote-shell的Spring Boot应用程序。 When I put this hello.groovy script it prints 'hello' and that's OK. 当我把这个hello.groovy脚本打印出'hello'时就可以了。
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. 但是当我尝试注入一些Spring bean时,它总是为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"})
我有@ComponentScan(basePackages={"com....", "commands"})
Spring BeanFactory can be taken from the Invocation context. Spring BeanFactory可以从Invocation上下文中获取。
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";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.