在 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 的信息?
最佳答案
将其放入/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
关于java - 在 CRaSH shell(Spring-boot 远程 shell)中过滤 Spring beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31025996/