akuneen 2022-06-22 09:59 采纳率: 87.5%
浏览 37
已结题

关于获取DefaultSingletionBeanRegistry的单例属性获取


//获取DefaultSingletonBeanRegistry下收集所有单例注册的属性
Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
//取消语言访问检查
singletonObjects.setAccessible(true);
//获取context下面的BeanFactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
从这里向下就看不懂了,为什么要get Factory
Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
map.entrySet().stream().forEach(info->{
    System.out.println(info.getKey()+"==="+info.getValue());
});
  • 写回答

2条回答 默认 最新

  • Tomshidi 2022-06-22 10:21
    关注

    Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
    这一句的意思是:从beanFactory对象中获取属性singletonObjects的值。
    Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects")。这里的singletonObjects只是一个类结构定义,真正获取值需要用具体实例对象去拿,比如传入这里的beanFactory = context.getBeanFactory() 这个factory对象。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月30日
  • 已采纳回答 6月23日
  • 创建了问题 6月22日