简体   繁体   中英

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

In the Spring-boot remote shell, CRaSH, I can dump all the Spring beans to the screen in what looks like 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 

... etc

But I can't find a way to filter that output:

 beans | filter -p bean:repositoryServer

I see from the internal "man" page that the beans command produces Object, and filter consumes Map , so the failure makes sense.

How can I get info about a single bean from the CRaSH shell?

Put this in a file under /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)
            }
        };
    }
}

You can the use it to find beans by name like

beans | bfilter .*Endpoint

or just find a single bean

beans | bfilter application

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM