在CRaSH shell中过滤Spring bean(Spring-boot远程shell)

时间:2015-06-24 12:02:44

标签: java spring-boot crash-shell

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

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

...等

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

 beans | filter -p bean:repositoryServer

我从内部" man" beans命令生成对象的页面,filter消耗Map,因此失败是有意义的。

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

1 个答案:

答案 0 :(得分:3)

将它放在/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