Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 298
CodeBlue Avatar asked Apr 24 '12 18:04

CodeBlue


People also ask

Is application context a Spring container?

The Application Context is Spring's advanced container. Similar to BeanFactory, it can load bean definitions, wire beans together, and dispense beans upon request.

What is Spring container and context?

The interface org. springframework. context. ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.

What is a Spring application context?

Spring ApplicationContext ApplicationContext is a corner stone of a Spring Boot application. It represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.

What is the difference between BeanFactory and ApplicationContext container?

While the BeanFactory provides basic functionality for managing and manipulating beans, often in a programmatic way, the ApplicationContext provides extra functionality like MessageSource, Access to resources, Event propagation to beans, Loading of multiple (hierarchical) contexts etc.


2 Answers

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.

like image 166
yorkw Avatar answered Oct 07 '22 05:10

yorkw


Application context is an implementation for IoC container.

like image 40
IraK Avatar answered Oct 07 '22 03:10

IraK