1

I am struggling with Architecture level of Spring web application, Below I am making my pointers. Please help regarding the below:

Context Level Programing: Servlet Context and Root Application Webapplication context and Web Application context.

  1. I don't know which bean needs to mention on which level.

  2. What is the Hierarchy level of Context in Spring Web Application. which comes upper or lower.

  3. Which context is under which context.

My Understanding is Servlet Context is the object of Servlet Container(tomcat container), and Servlet container is responsible to manage all the servlet, where all servlet lives.

All Bean dependecies Lives in Application container, and root web application context is the object of Application Container.

Every servlet have its own web application context, i don't know what is this, is it same as Servlet context.

4. And Relation between them?

Please describe it with help of diagram like this, (It will be really appreciable)

This is just example to understand which loads first and what is inside what. Please check this reference diagram

Yawar
  • 11
  • 5

1 Answers1

1

Think of context as a component's execution environment.

A servlet container (Web server), hosts web applications and on startup it creates one ServletContext for each of these applications. So each web app lives in a servlet context which provides to it information about its environment (container). This is Java Servlet API. Tomcat is an implementation of this API.

Now, Spring is one of the frameworks we can use to create web apps. When we deploy our web app in the container, it (container) will create a ServletContext and this is where our app will live in.

Each Spring application has a root context (ApplicationContext) and in it one or more child contexts can exist. WebApplicationContext extends ApplicationContext. There can be many WebApplicationContexts, children of the root context of an application. The WebApplicationContext (which adds a method, getServletContext() ) is able to work with the ServletContext in which it lives.

So in a spring based web app deployed in a container:

container --contains 1..n--> ServletContext --contains 1--> ApplicationContext --contains 1..n--> WebApplicationContext

martidis
  • 2,897
  • 1
  • 11
  • 13
  • Please can look at this question also coz its regarding loading the XML for creating the application context. [https://stackoverflow.com/questions/52816113/how-contextloaderlistner-loads-xml-file-for-application-context-spring-mvc] – Yawar Oct 16 '18 at 09:25
  • @Yawar I have to admit I don't understand the question. Can you please try to rephrase? – martidis Oct 16 '18 at 09:33
  • I am trying to ask you that, In Non-webapp application we do these things for loading the xml file and creating the Application Context. ApplicationContext context = new FileSystemXmlApplicationContext ("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml"); – Yawar Oct 16 '18 at 09:39
  • 2. Using ClassPathXmlApplicationContext . But when we load XML with ContextLoaderListener and we create application context, Which implementation ContextLoaderListener do or wt implementation ContextLoaderListener do internally – Yawar Oct 16 '18 at 09:43
  • Still not understand your question, but I ll attempt to respond based on what I think you might mean. When the web app gets loaded, web.xml will be parsed to discover servlets, filters, listeners, etc. Or do you mean as in the question you pointed me to, how to override onStartup using xml configuration instead of annotations/class? – martidis Oct 16 '18 at 10:06
  • I am really sorry that I am unable make u understand. Down the line, Thanks alot for your help and your effort. I really appreciate it. – Yawar Oct 16 '18 at 11:24