Spring中的beanFactory和ApplicationContext的有什么区别和关联
2015-03-10 10:58
274 查看
从上面的类结构图中可以看出来,ApplicationContext 是 BeanFactory接口的子接口
其中BeanFactory获得配置文件的实例是:
<span style="white-space:pre"> </span>// 使用BeanFactory 读取配置文件 BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); HelloService helloService4 = (HelloService) beanFactory.getBean("helloService"); helloService4.sayHello();
ApplicationContext获取配置文件实例的方法是:
<span style="white-space:pre"> </span>// 使用Spring Ioc 方式 获得HelloService 实例 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获得工厂实例 HelloService helloService2 = (HelloService) applicationContext.getBean("helloService"); // 通过id获得实例 //helloService2.setInfo("itcast"); // 已经配置依赖注入 helloService2.sayHello();
其实两个在代码看来就是在获取配置文件的时候 的差异,他们还有其他的差异:
1)BeanFactory 采用的是延迟加载,第一次getBean的时候才会初始化Bean
2)ApplicationContext是对BeanFactory的扩展,提供了更多的功能
国际化处理
事件传递
Bean自动装配
各种不同应用层的Context实现
结论:开发中尽量使用ApplicationContext 就可以了
相关文章推荐
- Spring之BeanFactory与ApplicationContext区别
- 《Spring 3.x 企业应用开发实战》学习笔记 第三章 ApplicationContext和BeanFactory区别
- Spring中ApplicationContext和beanfactory区别---解析二
- spring 后置处理器BeanFactoryPostProcessor和BeanPostProcessor的用法和区别
- Spring BeanFactory 与 FactoryBean的区别
- spring中的BeanFactory与ApplicationContext的作用和区别?
- Spring之BeanFactory与ApplicationContext区别
- Spring之BeanFactory与ApplicationConText区别
- spring 中ApplicationContext与beanfactory的区别
- 分析Spring的BeanFactory与ApplicationContext的getBean方法-----区别
- spring factoryBean 和 beanFactory 的区别
- Spring中BeanFactory与ApplicationContext的区别
- spring中的BeanFactory与ApplicationContext的作用和区别?
- Spring之BeanFactory与ApplicationConText区别
- java面试题三:spring中的BeanFactory与ApplicationContext的作用和区别?
- spring的userBean的作用域有哪些,分别是什么,区别又是什么?
- Spring中BeanFactory、ApplicationContext区别
- spring中的BeanFactory与ApplicationContext的作用和区别?
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
- Spring中ApplicationContext和beanfactory区别---解析一