spring笔记-StaticListableBeanFacto
2018-06-11 本文已影响35人
兴浩
StaticListableBeanFactory实现了BeanFactory和ListableBeanFactory
跟DefaultListableBeanFactory相比,可以认为是简化版的BeanFactory,由于未实现BeanDefinitionRegistry,所以并不支持BeanDefinition的注册操作
@Test
public void testFindsBeansOfTypeWithStaticFactory() {
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
TestBean t1 = new TestBean();
TestBean t2 = new TestBean();
DummyFactory t3 = new DummyFactory();
DummyFactory t4 = new DummyFactory();
t4.setSingleton(false);
lbf.addBean("t1", t1);
lbf.addBean("t2", t2);
lbf.addBean("t3", t3);
lbf.addBean("t4", t4);
Map<String, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, true);
assertEquals(4, beans.size());
assertEquals(t1, beans.get("t1"));
assertEquals(t2, beans.get("t2"));
assertEquals(t3.getObject(), beans.get("t3"));
assertTrue(beans.get("t4") instanceof TestBean);
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, DummyFactory.class, true, true);
assertEquals(2, beans.size());
assertEquals(t3, beans.get("&t3"));
assertEquals(t4, beans.get("&t4"));
beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, FactoryBean.class, true, true);
assertEquals(2, beans.size());
assertEquals(t3, beans.get("&t3"));
assertEquals(t4, beans.get("&t4"));
}