BeanFactory作为 IoC 容器示例
生活随笔
收集整理的這篇文章主要介紹了
BeanFactory作为 IoC 容器示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** {@link BeanFactory} 作為 IoC 容器示例*/
public class BeanFactoryAsIoCContainerDemo {public static void main(String[] args) {// 創建 BeanFactory 容器DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);// XML 配置文件 ClassPath 路徑String location = "classpath:/META-INF/dependency-lookup-context.xml";// 加載配置int beanDefinitionsCount = reader.loadBeanDefinitions(location);System.out.println("Bean 定義加載的數量:" + beanDefinitionsCount);// 依賴查找集合對象lookupCollectionByType(beanFactory);}private static void lookupCollectionByType(BeanFactory beanFactory) {if (beanFactory instanceof ListableBeanFactory) {ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;Map<String, User> users = listableBeanFactory.getBeansOfType(User.class);System.out.println("查找到的所有的 User 集合對象:" + users);}}}
?
總結
以上是生活随笔為你收集整理的BeanFactory作为 IoC 容器示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring IoC容器:BeanFac
- 下一篇: 注解能力ApplicationConte