DefaultSingletonBeanRegistry Author: HuiFer 源码阅读仓库: SourceHot-Spring 源码路径: org.springframework.beans.factory.support.DefaultSingletonBeanRegistry 官方提供的测试类: org.springframework.beans.factory.support.DefaultSingletonBeanRegistryTests 类图 注册方法解析 从名字可以看出这是一个单例对象的注册类 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerSingleton 测试用例出发 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Test public void testSingletons() { DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry(); TestBean tb = new TestBean(); beanRegistry.registerSingleton("tb", tb); assertSame(tb, beanRegistry.getSingleton("tb")); TestBean tb2 = (TestBean) beanRegistry.getSingleton("tb2", new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { return new TestBean(); } }); assertSame(tb2, beanRegistry.getSingleton("tb2")); assertSame(tb, beanRegistry.getSingleton("tb")); assertSame(tb2, beanRegistry.getSingleton("tb2")); assertEquals(2, beanRegistry.getSingletonCount()); String[] names = beanRegistry.getSingletonNames();……
阅读全文