My colleagues very often use word "application context". In many articles this collocation used very often too.
My current understanding: application context is single xml file.
But I understand that if I was right, people wouldn't use "application context" instead of configuration xml file.
Can you help me to deal with this issue?
You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.
applicationContext. xml defines the beans that are shared among all the servlets. If your application has more than one servlet, then defining the common resources in the applicationContext.
ApplicationContext is Spring's container. It's used to wire the configurations from Spring beans together and use them for the application. Use ApplicationContext if you want to retrieve the information of Spring beans. Use ServletContext if you want to get/set attribute those shared to all Servlet.
@feak gives a straight answer about the meaning of ApplicationContext
in terms of Spring. In short, it is an object that loads the configuration (usually a XML file annotation based) and then Spring will start managing the beans and its benefits:
To start an application context, you may use one of the following:
Manually load the application context at the beginning of your application. This is done for sample purposes or in standalone applications:
public class Foo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("path/to/applicationContext.xml"); //use the context as you wish... } }
In case of Java web applications using Spring MVC, the DispatchServlet
will load the application context for you, so you only have to create a springapp-servlet.xml file in WEB-INF folder of the application.
Note that an application context is associated to a single configuration (XML based or not). Period.
After understanding this, you could also understand that you can have more than a single application context per application. This is, having two or more ApplicationContext
s in the same application. From the last example in the console application, this is easy to check:
public class Foo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("path/to/applicationContext.xml"); ApplicationContext context2 = new ClassPathXmlApplicationContext("path/to/applicationContext.xml"); //use the context as you wish... } }
Note that we have two application contexts using the same XML configuration. Can you do this? Yes, you're actually seeing it here. What's the difference, then? The main difference is that Spring beans singleton scopes are singleton per application context, this mean when retrieving a Bar
bean that's configured in applicationContext.xml file from context
will not be the same as retrieving it from context2
, but several retrieves from context
will return the same Bar
bean instance.
Is this considered a good or bad practice? Neither, it will depend on the problem to be solved (in case of last example, I would say it is a bad practice). Most people would recommend having all your beans configured in a single place (via XML or another) and loaded by a single application context.
Let's understand this in simple words.
The ApplicationContext is the central interface within a Spring application that is used for providing configuration information to the application. It's created when the application starts running.
It provides the entire configuration needed by our application:
etc.
package com.srmhitter9062.spring; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ApplicationContextUtils implements ApplicationContextAware { private static ApplicationContext ctx; @Override public void setApplicationContext(ApplicationContext appContext) throws BeansException { ctx = appContext; } public static ApplicationContext getApplicationContext() { return ctx; } }
We can get some idea about the Application object in below snapshot.
In summary, we can say the Application context is a Configuration object created for application to run.
The applicationContext.xml
defines the beans for the "root webapp context". It's a web aware ApplicationContext.
It is used to have beans that are shared between all servlets in a web application.
I hope this is helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With