title: The difference between web.xml, beans.xml, applicationContext.xml, etc
body: <p>I've been working with Spring MVC for a while now creating my projects in Netbeans running on Glassfish servers. While everything is working fine I feel like I lack understanding on just what should be in each of the XML files - and in several cases this has led to me just trying a chunk of XML in each file one after another until it works.</p>
  
  <p>I've not been able to find any clear description of this on Google and I've tried a few times.</p>
  
  <p>I'll detail my current understanding here and then if anyone can follow up with a more detailed explanation or let me know where I'm mistaken that would be much appreciated.</p>
  
  <p><strong>web.xml</strong></p>
  
  <p>This seems to be configuring the servlet container by telling it what classes to use for handling queries. The confusion seems to be that while configuring Spring in here does not work - you need to put some configuration in here to install Spring i.e.</p>
  
  <pre><code>&lt;filter&gt;
    &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
    &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
    &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>You add that in <em>web.xml</em> to get Spring Security working - but then you actually configure Spring Security in another file.</p>
  
  <p><strong>dispatcher-servlet</strong></p>
  
  <p>This seems to be similar to <em>web.xml</em> in that it is about configuring the servlet container to enabling Spring - but in my projects it is mostly empty and just contains a single <code>viewResolver</code>. What should go in here and how does it differ from <em>web.xml</em>?</p>
  
  <p><strong>beans.xml</strong></p>
  
  <p>At the moment this file is empty apart from an xml root tag <code>&lt;beans&gt;</code> and a few namespace/schema definitions in all my projects. Is it actually needed for anything?</p>
  
  <p>Is <code>bean-discovery-mode="annotated"&gt;</code> in the root tag the reason it is empty?</p>
  
  <p><strong>applicationContext</strong></p>
  
  <p>This seems to be where all the actual Spring configuration goes such as <code>&lt;mvc:annotation-driven /&gt;</code>, <code>&lt;context:component-scan /&gt;</code> etc.</p>
  
  <p>You can also split this configuration up into multiple files and use <code>&lt;import /&gt;</code> to link those files into the application context.</p>
  
  <p><strong>glassfish-web</strong></p>
  
  <p>I've mostly been ignoring this file, is there any reason I shouldn't?</p>
  
  <p><strong>The questions</strong></p>
  
  <p>So really the questions are:</p>
  
  <ul>
  <li>What have I missed from the above?</li>
  <li>Why is there a separate <em>beans.xml</em> that seems to do nothing? Is it a legacy from before annotation driven was brought in?</li>
  <li>Why are there both <em>dispatcher-servlet.xml</em> and <em>web.xml</em> and what is the difference between them?</li>
  <li>How is <em>glassfish-web.xml</em> different from those two?</li>
  <li>How do I tell whether a fragment of xml should go into which of these files without trying it in them all until it works? (The rough rule of thumb I've developed so far is "spring config in <em>applicationContext.xml</em>, installing spring components in <em>web.xml</em>, ignore the other files"!)</li>
  </ul>
  
  <p>Thanks in advance,</p>
  
  <p>Tim</p>