在 CRaSH shell 中过滤 Spring 个 bean(Spring-boot remote shell)

Filter Spring beans in CRaSH shell (Spring-boot remote shell)

在 Spring-boot remote shell, CRaSH 中,我可以将所有 Spring beans 转储到屏幕上,看起来像 JSON:

 > beans
 [{context=application, parent=null, beans=[{bean=repositoryServer, scope=singleton,
  type=com.opentext.tlsPolicyRepository.RepositoryServer$$EnhancerBySpringCGLIB$1a12c7, 
 resource=null, dependencies=[]}, {bean=tlsPolicyRepositoryController,
 scope=singleton, type=com.opentext.tlsPolicyRepository.TlsPolicyRepositoryController,
 resource=file 

...等等

但我找不到过滤该输出的方法:

 beans | filter -p bean:repositoryServer

我从内部"man"页面看到beans命令产生了Object,而filter消耗了Map,所以失败是有道理的。

如何从 CRaSH shell 获取有关单个 bean 的信息?

将其放入 /src/main/resources/commands/bfilter 下的文件中。groovy

import org.crsh.cli.Command;
import org.crsh.cli.Usage;
import org.crsh.command.Pipe;
import org.crsh.cli.Argument;

class bfilter {
 @Usage("Filters output of beans")
    @Command
    Pipe<Object,Object> main(
    @Usage("regex of bean names to find")
    @Argument String regex) {
        return new Pipe<Object, Object>(){
            public void provide(Object result) {
                def bean = []
                for(entry in result){
                    for(aBean in entry.beans){
                        if(aBean.bean.matches(regex)){
                            bean << aBean
                        }
                    }

                }
                context.provide(bean)
            }
        };
    }
}

您可以使用它按名称查找 bean,例如

beans | bfilter .*Endpoint

或者只找到一个 bean

beans | bfilter application

Source