【问题标题】:Filter Spring beans in CRaSH shell (Spring-boot remote shell)在 CRaSH shell 中过滤 Spring beans(Spring-boot 远程 shell)
【发布时间】:2015-06-24 12:02:44
【问题描述】:

在 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 命令生成Object,而filter 消耗Map,所以失败是有道理的。

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

【问题讨论】:

    标签: java spring-boot crash-shell


    【解决方案1】:

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

    或者只找一个豆子

    beans | bfilter application
    

    Source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多