Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are right - there are two different application contexts, the root application context loaded up by ContextLoaderListener (at the point the ServletContext gets initialized), and the Web Context (loaded up by DispatcherServlet), the root application context is the parent of the Web context.</p> <p>Now, since these are two different application contexts, they get acted on differently - if you define <code>component-scan</code> for your services in application context, then all the beans for the services get created here.</p> <p>When your Dispatcher servlet loads up it will start creating the Web Context, at some point(driven by <code>&lt;mvc:annotation-driven/&gt;</code> it will create a mapping for your uri's to handler methods, it will get the list of beans in the application context(which will be the web application context, not the Root application Context) and since you have not defined a <code>component-scan</code> here the controller related beans will not be found and the mappings will not get created, that is the reason why you have to define a component-scan in the dispatcher servlets context also.</p> <p>A good practice is to exclude the Controller related beans in the Root Application Context:</p> <pre><code>&lt;context:component-scan base-package="package"&gt; &lt;context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/&gt; &lt;/context:component-scan&gt; </code></pre> <p>and only controller related one's in Web Application Context:</p> <pre><code>&lt;context:component-scan base-package="package" use-default-filters="false"&gt; &lt;context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" /&gt; &lt;/context:component-scan&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload