Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

Tags:

java

spring

  • Are applicationContext.xml and spring-servlet.xml related anyhow in Spring Framework?
  • Will the properties files declared in applicationContext.xml be available to DispatcherServlet?
  • On a related note, why do I need a *-servlet.xml at all? Why is applicationContext.xml alone insufficient?
like image 501
user448070 Avatar asked Sep 06 '10 14:09

user448070


People also ask

What is Spring ApplicationContext xml?

Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application. Spring-servlet.

What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

ApplicationContext is used to create standalone applications. WebApplicationContext is used to create web applications.

What is the difference between Servletcontext and ApplicationContext?

They are separate things. Every Java web applications based on Servlet technology will have a servlet context, whether it's a spring application or not. In contrast, the ApplicationContext is a Spring thing; in very simple terms, it's a container to hold Spring beans.

Where is ApplicationContext xml in Spring boot?

src/main/resources/applicationContext.xml: properties in src/main/resources too.


1 Answers

Spring lets you define multiple contexts in a parent-child hierarchy.

The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.

The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).

Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.

All Spring MVC controllers must go in the spring-servlet.xml context.

In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.

like image 103
skaffman Avatar answered Sep 22 '22 21:09

skaffman