gpt4 book ai didi

spring - Spring 应用程序上下文和 Spring 容器有什么区别?

转载 作者:IT老高 更新时间:2023-10-28 13:53:57 29 4
gpt4 key购买 nike

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

最佳答案

来自 this link 的回答Ajinkya 所附的内容非常全面,但是,我想引用其他 Material 的一些优点 - Spring in Action (Manning Publications):

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).

enter image description here

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.

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:

  • 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.

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.

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 应用程序上下文和 Spring 容器有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10303413/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com