BeanFactory和ApplicationContext区别

1.ApplicationContext

public void testApplicationContext() {
    String configPath = "com/boomsecret/ioc/bean.xml";
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configPath);
    UserDao userDao = applicationContext.getBean(UserDao.class);
    userDao.save();
}

2.BeanFactory

public void testBeanFactory() {
    String configPath = "com/boomsecret/ioc/bean.xml";
    Resource resource = new ClassPathResource(configPath);
    BeanFactory beanFactory = new XmlBeanFactory(resource);
    UserDao userDao = beanFactory.getBean(UserDao.class);
    userDao.save();
}

3.总结

ApplicationContext会加载bean.xml所有的class并实例化,BeanFactory只会按需加载即调用getBean()才会实例化对象。

你可能感兴趣的:(BeanFactory和ApplicationContext区别)