• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

ApplicationContext vs WebApplicationContext and ServletContext  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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:

web.xml
------------



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

myApplicationContext.xml
--------------------------------


myWebApplicationContext.xml
-----------------------------------




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


 
Greenhorn
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
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mechanically, given your MyDispatcherServlet is a MVC DispatcherServlet, you can use
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!