Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spring multiple ApplicationContext

The definition of the spring ApplicationContext is very ambiguous, I almost finish a whole book of tutorial but still cannot understand what is the ApplicationContext stand for.

According the Spring API, ApplicationContext is:

  • Central interface to provide configuration for an application. This is read-only while the application is running, but may be reloaded if the implementation supports this.

  • The root interface for accessing a Spring bean container. This is the basic client view of a bean container.

From above, my questions are:

1) I keep seeing the book mentioned "container", what is the container refer to? One container does it mean one java process? or one container refer to one ApplicationContext object?

2) If i instantiate two ApplicationContext in one java application (both in main body), are these two interfaces to one central container? Or two separate instances? See the code below, what is the difference between context1 and context2? If there is a Singleton in Beans.xml, it is invoked by context1 and context2, are they two separated instances or the same instance?

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");
like image 783
Sam YC Avatar asked Apr 25 '15 08:04

Sam YC


People also ask

Can we have multiple ApplicationContext in Spring?

You can have two contexts within an application. If you have two contexts each will have its own singleton.

Can we have two context path in Spring boot?

In order to have multiple context paths, you're limited to deploying the application multiple times with that convenient property set to what you want for each deployment. However, this is resource expensive because you're spinning up two applications for every one application you would normally spin up.

How does ApplicationContext work in Spring?

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. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata.

Is it possible to have more than one application context in spring?

It is common to have more than one application context in a Web application, because Spring has a notion of hierachies of ApplicationContext. You could declare them as : Here you can retrieve from context1 only beans declared in it, but from context2 you will retrieve beans from context2 and context1.

How do I create a Spring container using applicationcontext in Java?

Let's write code to create a Spring container using the ApplicationContext interface: ApplicationContext context = new ClassPathXmlApplicationContext ( "applicationContext.xml" ); Note that we are supplying configuration metadata via applicationContext.xml file (XML-based configuration).

When to use applicationcontext class in Spring Boot?

This class is useful when we need to load the ApplicationContext programmatically. In general, test harnesses and standalone applications are some of the possible use cases for this. For example, let's see how we can create this Spring container and load the beans for our XML-based configuration: 5.5. ClassPathXmlApplicationContext

What is the use of XML application context in spring?

FileSystemXmlApplicationContext is used to load XML-based Spring Configuration files from the file system or from URL. We can get the application context using Java code. It is useful for standalone environments and test harnesses. The following code shows how to create a container and use the XML as metadata information to load the beans.


5 Answers

First you questions :

1) I keep seeing the book mentioned "container", what is the container refer to? One container does it mean one java process? or one container refer to one ApplicationContext object?

The ApplicationContext is the central interface, but the underlying container is a BeanFactory. More exactly, BeanFactory is a lower level interface implemented by all Application contextes from which you get the Beans. In that sense, I think that the word container stands here for the BeanFactory - one per ApplicationContext.

2) If i instantiate two ApplicationContext in one java application (one Main body), are these two interface to one central container? Or two separate different instance? See the code below, what is the difference between context1 and context2? If there is a Singleton in Beans.xml, it is invoked by context1 and context2, are they two separated instance or same instance?

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");>

With that instanciations, you will get 2 totally independent application contexts. One bean declared in first will not be found in the other.

BUT

It is common to have more than one application context in a Web application, because Spring has a notion of hierachies of ApplicationContext. You could declare them as :

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml", context1);>

Here you can retrieve from context1 only beans declared in it, but from context2 you will retrieve beans from context2 and context1. Specifically, beans are first looked for in context2 and if not found then looked for in context1.

This is used in Spring MVC where you normally have one root context (for all beans not directly related to the MVC DispatcherServlet) and one child context dedicated to the DispatcherServlet that will contain the beans for controllers, views, interceptors, etc.

like image 144
Serge Ballesta Avatar answered Sep 24 '22 06:09

Serge Ballesta


By container they refer to the core spring Inversion of Control container. The container provides a way to initialize/bootstrap your application (loading the configuration in xml files or annotations), through use of reflection, and to manage the lifecycle of Java objects, which are called beans or managed objects.

During this initial phase, you do not have (normally, yet it is possible) control in your application, instead you will get a completely initialized state for the application when the bootstrapping is done (or nothing, in case it fails).

It is a replacement, or possibly an addition, to what is called an EJB3 container; yet, the spring provided one fails to adhere to the EJB defined standard. Historically, adoption of EJB has been limited by the complexity of that specification, with spring being a newly created project for having EJB3 comparable features running on a J2SE jvm and without an EJB container, and with much easier configuration.

ApplicationContext (as an interface, and by the direct implementation flavours) is the mean of implementing this IoC container, as opposed to the BeanFactory, which is now (a sparsely used and) more direct way of managing beans, which by the way provides the base implementation features for the ApplicationContext.

As per your second question, you can have multiple instances of ApplicationContexts, in that case, they will be completely isolated, each with its own configuration.

like image 45
guido Avatar answered Sep 22 '22 06:09

guido


I keep seeing the book mentioned "container", what is the container refer to? One container does it mean one java process? or one container refer to one ApplicationContext object?

Here container means spring container which is nothing but ApplicationContext. This internally reads spring configuration and loads the classes based on configuration. You may think it as SpringProcessor which provides the various functionality like bean initialization, injection, i18n, bean Post processing etc off the shelf

with

ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("Beans.xml");

There will be two conatiners , hence two singleton beans. Here singleton means singleton instance per container. Ideally you should have only one container until and unless you have a reason for two. For learning purpose it makes sense to understand the concepts

like image 30
M Sach Avatar answered Sep 25 '22 06:09

M Sach


ApplicationContext is an implementation of a Spring container. In simple words Spring container manages the entire Spring application via an ApplicationContext. Spring container via ApplicationContext manages the life cycle of a Spring bean i;e from initiation to destruction.

A spring container is nested inside a J2EE container.

I keep seeing the book mentioned "container", what is the container refer to? One container does it mean one java process? or one container refer to one ApplicationContext object?

A container manages the life cycle of an object. Tomcat is a an example of a container. Just like Spring container manages the app via ApplicationContext a J2EE container Tomcat manages the app via web.xml

A container provides communications support. Security in a web application. JSP support, Internationalization, event-propagation & many other features. It supports multithreading, spawns a new thread for every request for a resource. You don't have to explicitly write the code for that. Just like a spring container, a J2ee container manages a servlet life cycle.

If i instantiate two ApplicationContext in one java application (one Main body), are these two interface to one central container?

If you want to instantiate multiple ApplicationContexts in your application. It will be in a parent child hierarchy. There will be one root ApplicationContext & then there will be child ApplicationContext respective to every DispatcherServlet. Beans global to the application will be defined in the root ApplicationContext. All the ApplicationContexts will be managed by only one spring container.

like image 45
underdog Avatar answered Sep 23 '22 06:09

underdog


  1. container is a Java object, an instance of one of ApplicationContext implementations like ClassPathXmlApplicationContext.

  2. It is 2 different containers, if Beans.xml has a singleton bean B1, then context1.getBean("B1") and context2.getBean("B1") will return 2 different instances of B1

like image 43
Evgeniy Dorofeev Avatar answered Sep 23 '22 06:09

Evgeniy Dorofeev