在Groovy bean中注入Spring bean [英] Inject Spring bean in groovy bean

查看:1366
本文介绍了在Groovy bean中注入Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有spring-boot-starter-remote-shell的Spring Boot应用程序。
当我把这个hello.groovy脚本打印出'hello'并且没问题。

 包命令

import org.crsh.cli.Usage
import org.crsh.cli.Command

class hello {

@Usage(Say Hello )
@Command
def main(InvocationContext context){
returnhello;
}

}

但是当我尝试注入一些

 包命令

导入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; b
$ b @Usage(Say Hello)
@Command
def main(InvocationContext context){
if(jobLauncher!= null){
return 好;
} else {
returnNULL;
}
返回hello j;
}

}

我有 @ComponentScan(basePackages = {com ....,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
$ b $ 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 {
returnNULL;
}
返回hello j;
}

}


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";
    }

}

这篇关于在Groovy bean中注入Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆