What is the difference between a Spring application context and a Spring container?

前端 未结 2 1827
攒了一身酷
攒了一身酷 2021-01-31 05:13

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 \"ApplicationCon

相关标签:
2条回答
  • 2021-01-31 06:00

    Application context is an implementation for IoC container.

    0 讨论(0)
  • 2021-01-31 06:03

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

    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.

    0 讨论(0)
提交回复
热议问题