//获取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());
});
Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
这一句的意思是:从beanFactory
对象中获取属性singletonObjects
的值。Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects")
。这里的singletonObjects
只是一个类结构定义,真正获取值需要用具体实例对象去拿,比如传入这里的beanFactory = context.getBeanFactory()
这个factory对象。
//获得单例的对象强转为map类型
Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);
//循环输出map的key与value的值
map.entrySet().stream().forEach(info->{
System.out.println(info.getKey()+"==="+info.getValue());
});