Evg*_*rov 5 java groovy spring
我有Spring-boot-starter-remote-shell的Spring Boot应用程序.当我把这个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";
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试注入一些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";
}
}
Run Code Online (Sandbox Code Playgroud)
我有 @ComponentScan(basePackages={"com....", "commands"})
Spring BeanFactory 可以从调用上下文中获取。
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";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1729 次 |
| 最近记录: |