开发者

Spring framework - Preloading beans and looping through them without knowing names

Is there a w开发者_Python百科ay I can preload all beans from the XML file at once and then loop through them without knowing their bean names? I've seen that it is obviously possible to do the preloading but I haven't seen a way to access them without knowing their specific bean names. Thanks!


You can use getBeanDefinitionNames() from the XmlBeanFactory class. From Java Docs:-

Return the names of all beans defined in this factory.

Does not consider any hierarchy this factory may participate in, and ignores any singleton beans that have been registered by other means than bean definitions.

Example:

ClassPathResource context = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(context);
String[] beans = factory.getBeanDefinitionNames();
//loop through beans array to get bean names
for (int i=0; i<beans.length; i++) { 
   //do your stuff
}

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜