application context. What is this?
Asked Answered
A

4

47

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?

Aprilaprile answered 27/10, 2013 at 8:14 Comment(0)
H
39

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

  • Beans declared in package
  • Beans declared by annotations
  • Constructor and method autowiring
  • Bean injection
  • Configuration, .properties and .yaml file loading
  • etc

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

Horwath answered 27/10, 2013 at 8:38 Comment(8)
Can you explain differences between single application context and single xml fileAprilaprile
@Aprilaprile what don't you understand? The XML file defines the configuration, the application context loads the configuration from this file and Spring start handling your defined beans.Horwath
One context can uses many xml files. How? I don't understand when I use many contexts and when I use single contextAprilaprile
How Can I understand to which context this xml concerns?Aprilaprile
it is helpfull but I have more questions. What about differences ApplicationContext and WebApplicationContext ?Aprilaprile
@Aprilaprile you should use your favorite SEO (e.g. google) to search for this. As you may read in WebApplicationContext javadoc, it is an interface that extends from ApplicationContext and is used in web applications internally by DispatchServlet.Horwath
last question please. all beans from ApplicationContext exist in WebApplicationContext?Aprilaprile
@Aprilaprile if you understand what's the application context and you understand that the web application context is just an application context for a web application, then you would realize that all the beans in the application context will exists in the web application context as well (assuming they load the configuration from the same source). How to test this? Create a simple XML configuration, create two projects, a console one and a java web one, use the same configuration for both (following the respective rules), then test the existence of the beans on each.Horwath
R
6

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:

  1. Bean Factory - Responsible for creation of java objects called beans. One example is components in the application.
  2. Application listeners - all listeners needed for events.
  3. WebServer information.
  4. Application current environment specific information.
  5. Resource pattern resolver - resource loader with path matcher.
  6. Life cycle Processor.
  7. Class Loader.
  8. Start and shutdown monitor.
  9. Servlet Context.
  10. Reader and Scanner.
  11. Logger

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.

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.

Relly answered 28/1, 2020 at 8:4 Comment(0)
B
1

I guess that you colleagues meant the loaded spring application context, which allows access to:

  • configuration of application,
  • initialized beans,
  • application events api,
  • etc
Banerjee answered 27/10, 2013 at 8:22 Comment(3)
which differences between xml file and "application context"Aprilaprile
Application context and spring xml file it is different terms. One application context can be initialized using one, two and more xml files. Also, to initialization application context can be used /@Configuration classes without xml files. So, i want to say that xml and /@Configuration it's approach to configurate context.Banerjee
The application can have several application contexts. Each context will have certain settings. there are tasks that require such an approach(several contexts).Banerjee
C
0

From the javadoc:

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.

An ApplicationContext provides: [...]

Carbonaceous answered 23/8, 2022 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.