可以看出BeanFactory表面上只有getBean相关的方法。
实际上控制反转、基本的依赖注入、Bean的生命周期的各种功能,都是由BeanFactory的实现类来实现的。(DefaultListableBeanFactory)
DefaultListableBeanFactory管理单例对象DefaultSingletonBeanRegistry。
通过反射获取singletonObjects的值
@Slf4j
@SpringBootApplication
@ServletComponentScan
@EnableTransactionManagement
public class ReggieApplication {public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {ConfigurableApplicationContext context = SpringApplication.run(ReggieApplication.class, args);Field singletonObjects = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");singletonObjects.setAccessible(true);ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();Map<String, Object> map = (Map<String, Object>) singletonObjects.get(beanFactory);map.forEach((k, v) -> System.out.println("-->"+k+":"+v));map.entrySet().stream().filter(e -> e.getValue().toString().startsWith("com.xkj")).forEach(e -> {System.out.println("==>"+e.getKey()+"="+e.getValue());});}}