Spring应用程序上下文和Spring容器有什么区别? [英] What is the difference between a Spring application context and a Spring container?

查看:661
本文介绍了Spring应用程序上下文和Spring容器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这两个概念有点困惑.阅读Spring文档,我发现了例如.那个豆工厂是Spring容器.我还读到"ApplicationContext是BeanFactory的完整超集".但是两者之间的区别并不明显.那么区别是什么呢?

I am little confused with these two concepts. Reading the Spring documentation, I found out, for eg. that bean factories are Spring containers. I also read that "ApplicationContext is a complete superset of the BeanFactory". But the difference between the two is not readily apparent. So what is the difference?

推荐答案

此答案Ajinkya附加的链接非常全面,但是,我想参考另一种材料-Spring in Action(Manning Publications)的一些要点:

Answers from this link attached by Ajinkya is pretty comprehensive, however, I would like to refererence some good points from another material - Spring in Action (Manning Publications):

在基于Spring的应用程序中,您的应用程序对象将存在于Spring容器中.如图2.1所示,容器将创建对象,将它们连接在一起,对其进行配置,并管理从摇篮到坟墓(或视情况而定,对于finalize()而言是全新的)的整个生命周期.

In a Spring-based application, your application objects will live within the Spring container. As illustrated in figure 2.1, the container will create the objects, wire them together, configure them, and manage their complete lifecycle from cradle to grave (or new to finalize() as the case may be).

没有单个Spring容器. Spring附带了几个容器实现,可以将它们分为两种不同的类型. Bean工厂(由org.springframework.beans.factory.BeanFactory接口定义)是最简单的容器,为DI提供基本支持.应用程序上下文(由org.springframework.context.ApplicationContext接口定义)通过提供应用程序框架服务(例如,从属性文件解析文本消息的能力以及将应用程序事件发布到感兴趣的对象的能力)建立在bean工厂的概念上.事件监听器.

There is no single Spring container. Spring comes with several container implementations that can be categorized into two distinct types. Bean factories (defined by the org.springframework.beans.factory.BeanFactory interface) are the simplest of containers, providing basic support for DI. Application contexts (defined by the org.springframework.context.ApplicationContext interface) build on the notion of a bean factory by providing application framework services, such as the ability to resolve textual messages from a properties file and the ability to publish application events to interested event listeners.

表面上,ApplicationContext与BeanFactory几乎相同.两者都装入bean定义,将bean装在一起,并根据请求分配bean.但是ApplicationContext提供了更多功能:

On the surface, an ApplicationContext is much the same as a BeanFactory. Both load bean definitions, wire beans together, and dispense beans upon request. But an ApplicationContext offers much more:

  • 应用程序上下文提供了一种解决文本消息的方法,包括对这些消息的国际化(I18N)的支持.
  • 应用程序上下文提供了一种加载文件资源(例如图像)的通用方法.
  • 应用程序上下文可以将事件发布到注册为侦听器的bean.
  • Application contexts provide a means for resolving text messages, including support for internationalization (I18N) of those messages.
  • Application contexts provide a generic way to load file resources, such as images.
  • Application contexts can publish events to beans that are registered as listeners.

由于它提供了其他功能,因此在几乎所有应用程序中,ApplicationContext都比BeanFactory更受青睐.您可能唯一考虑使用BeanFactory的情况是在资源稀缺的情况下,例如移动设备.

Because of the additional functionality it provides, an ApplicationContext is preferred over a BeanFactory in nearly all applications. The only times you might consider using a BeanFactory are in circumstances where resources are scarce, such as a mobile device.

除了应用程序上下文提供的其他功能之外,应用程序上下文和Bean工厂之间的另一个大差异是如何加载单例Bean. Bean工厂会延迟加载所有Bean,从而延迟Bean的创建,直到调用getBean()方法为止.应用程序上下文会更智能一些,并在上下文启动时预加载所有单例bean.通过预加载单例bean,可以确保它们在需要时可以随时使用-您的应用程序不必等待它们被创建.

Aside from the additional functionality offered by application contexts, another big difference between an application context and a bean factory is how singleton beans are loaded. A bean factory lazily loads all beans, deferring bean creation until the getBean() method is called. An application context is a bit smarter and preloads all singleton beans upon context startup. By preloading singleton beans, you ensure that they will be ready to use when needed—your application won’t have to wait for them to be created.

这篇关于Spring应用程序上下文和Spring容器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆