日期:2014-05-20  浏览次数:20637 次

Spring的问题!
beanOfType(ListableBeanFactory lbf, Class type) 

谁能详细解释下这个方法!谢谢!

------解决方案--------------------

查找在AC=ApplicationContext里面配置的所有SpringExample.HelloBean类型的bean。注意AC是Root ApplicationContext:
Object test = org.springframework.beans.factory.BeanFactoryUtils
.beanOfType(AC, SpringExample.HelloBean.class);


这也是spring从配置文件当中确定各个特殊bean的方式:
Spring怎么知道某个bean的配置就是urlMapping?另一个bean的配置就是viewResolver?
其实很简单,把所有的bean全部读到内存里,然后通过bean的名字或类型去找就行了。通过名字去找就是简单的getBean方法,通过类型去找则使用了BeanFactoryUtils.beansOfTypeIncludingAncestors的静态方法。
上面把beanOfType改成beansOfTypeIncludingAncestors也一样。