在Spring-boot远程外壳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的信息?
最佳答案
将其放在/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)
}
};
}
}
您可以使用它来按名称查找咖啡豆,例如
beans | bfilter .*Endpoint
或只是找到一个豆
beans | bfilter application
Source