• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

ApplicationContext vs WebApplicationContext and ServletContext  RSS feed

Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found in Spring spec the following sentence:
"Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope."

According that sentence I assume that every bean defined in ApplicationContext should be overwritten by beans with the same name in WebApplicationContext ?
Thus could anybody explain me why I got the error in the following code:

First I declared my ApplicationContext, WebApplicationContext and my own DispatcherServlet in web.xml:


Then I created bean "user" in AppContext and the same in WebAppContext:



At the end I implemented my version of DispatcherServlet where I retrieved bean "user":

When I run the code I got userName: TOM. So the bean "user" was taken from ApplicationContext - and this is my first WHY ? (it should be taken from WebAppContext so I should get value "JOHN")

After that I change the bean name in AppContext to userBis. And during running the code I got: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'user' is defined
And this is my second WHY? - it means that beans from WebAppContext weren't loaded ?

Thanks for any explanations.

Posts: 1
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not 100% sure - please correct me if You know better - but in fact myApplicationContext.xml is Your ROOT WebApplicationContext, not just ApplicationContext.

Now take a look at javadoc of WebApplicationContextUtils.getWebApplicationContext(servletContext):

Find the ROOT WebApplicationContext for this web application, which is typically loaded via ContextLoaderListener or ContextLoaderServlet.

That is why it gives You application context from myApplicationContext.xml instead of myWebApplicationContext.xml.
Ranch Hand
Posts: 491
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mechanically, given your MyDispatcherServlet is a MVC DispatcherServlet, you can use
a wee bit from the empire
global solutions you can do at home or in your backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!