• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

What is ApplicationContext and why is it needed ?

 
Ranch Hand
Posts: 182
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am reading a Spring 4 book. It mentions ApplicationContext. I know its an interface. But, what is this ApplicationContext really and what purpose does it
serve (in plain english please) ? Why is the word application used ? Why not use SpringContext or UserContext ?

 
author & internet detective
Posts: 40549
825
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ApplicationContext is where all your beans live. It is available anywhere within your application. (Programmers are typically called applications once they get bigger.)

UserContext wouldn't be a good name because it isn't user specific. For example, when you visit this website, you have a user session with your id and some other info. That's user information. The ApplicationContext has global information like the names of the forums.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Application context is actually a bad name, IMO. If you are talking about web applications, application context has a specific meaning, and you can store beans in web application context. Funny thing is that spring allows you to scope your beans to the application context. So, it becomes very confusing when you talk about beans declared in the application context, and store in the application context


FWIW, I calls the spring's application context as spring context
 
Ali Gordon
Ranch Hand
Posts: 182
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:Application context is actually a bad name, IMO. If you are talking about web applications, application context has a specific meaning, and you can store beans in web application context. Funny thing is that spring allows you to scope your beans to the application context. So, it becomes very confusing when you talk about beans declared in the application context, and store in the application context


FWIW, I calls the spring's application context as spring context



Now I am a little confused
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, exactly!!

Spring itself is an IoC and DI container. Basically, you have a spring XML file that describes which beans to instantiate , and which beans to inject into other beans. To make Spring create instances of the beans, you have to create an instance of spring application context, and give it the XML file. The Spring's application context parses the XML file, and starts instantiating the beans, and doing all the injection, and autoproxying, etc. As it is creating the beans, by default, it puts a reference to those beans in an internal Map. After it has finished creating beans, the Map will have all the beans. So, basically, APplicationCOntext is a facade over many things. THe most important things is that ApplicationCOntext has :- a) all the spring initialization logic that is called when the xml is loaded, b) all the beans that are loaded as part of the Spring initialization

If you were writing a command line application, you would generally use ClassPathXMLApplication to load the beans, and then get the bean from the ClassPathXMLApplication and call a method on it

Now, what happens in Spring MVC, you declare a DispatcherServlet in your web.xml. Internally, DispatcherServlet creates a Spring Application Context to load all the Spring beans. On initialization of the servlet, the Spring Application Context is loaded. When it gets a Get/Post request, it looks at the configuration to find which controller bean can serve that request, gets the controller bean from the Spring Application Context, and calls it.

Now, as per J2EE standard, the Servlet itself has a context. The way it works is very similar to Spring's applicationCOntext. You can create beans and store them in the Servlet Context. Normally, you don't access the Servlet Context from Spring MVC applications. But, if you do, it can get a little confusing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic