Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8451211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:10:36+00:00 2026-06-10T11:10:36+00:00

With Spring/MVC I often struggle determining which context I should define beans, either the

  • 0

With Spring/MVC I often struggle determining which context I should define beans, either the root-context.xml or the servlet-context.xml. What’s caused the confusion is I haven’t seen any documentation on the subject as to what to put where, in the spring sample code I often see the same thing defined in different contexts between samples.

As a rule of thumb I’ve presently defined anything that’s a service or a component in the root context, and reserved the servlet context for web controllers, interceptors and anything that’s only really related to web.

But what about security? I’ve presently defined it as an include of the root context, but is this correct?

Am I right in my understanding that the web context is actually a child context of the application root context?

Why do we need the web context to be separate?

I’ve read the spring documentation from 3.0 a couple of years ago but can’t remember anything specific to this, I’ve also read Spring in Action, Third Edition. I would love any material surrounding this topic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:10:37+00:00Added an answer on June 10, 2026 at 11:10 am

    There can and is usually a difference.

    Basically you applicationContext.xml is your root context and is where your service and data layer beans live.

    *-servlet.xml or webmvc-config.xml are special in that they:

    • Have a DispatcherServlet associated
    • Are alway WebApplicationContext bean factory
    • Are a child of the root context (which can also be a WebApplicationContext)

    But the biggest reason people do this is for unit testing, decoupling frontend from backend, and for separate view resolvers and/or multiple dispatcher servlets.

    It’s good for unit testing because you are loading less beans for testing your service layer. As I stated in my comments below I usually load my real applicationContext.xml like:

    @ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext.xml")
    

    Also because the servlet context requires a dispatcher servlet you need to register it as servlet like:

    <servlet>
        <servlet-name>my-web</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    

    So while you maybe able to load our MVC controller beans with the root context they aren’t really registered unless a dispatcher servlet is controlling that context. Although in theory I think the DispatcherServlet can load configs in the classpath notice that the contextConfigLocation value is not in the classpath.

    Also some people need multiple dispatcher servlets because of some of the limitations in that you can typically only have one resolver chain (view, locale, theme, etc…).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Spring MVC allows to define handler methods with a variety of parameters which are
In Spring MVC a Dispatcher servlet is used as a front controller to handle
In Spring MVC I can access my beans in JSP using JstlView's exposedContextBeanNames (or
In Spring MVC 2.5 with Jetty - probably with any servlet container -, I
I have a pretty simple JSP/Servlet 3.0/Spring MVC 3.1 application. On one of my
In Spring MVC 3.0, there is a class org.springframework.validation.BindingResult which finds the form parameters.
I've mapped the Spring MVC dispatcher as a global front controller servlet on /*
In Spring MVC, one can define interceptors that can perform work before and after
Using Spring mvc-3. I am writing a custom Converter which needs access to other
I use Spring MVC with java config. And I have two context configuration classes:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.