Note that there are some explanatory texts on larger screens.

plurals
  1. POThe difference between web.xml, beans.xml, applicationContext.xml, etc
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COHi, I can't give you a complete answer, but I have some suggestions which might help you. Before figure out the differences of these files, I think it's better to understand the definitions of Java Web Application, Java Web Server, Java Framework(Spring) and IDE(Netbean), they are totally different things. Once you got it, then you will find the different usages of these files.
      singulars
    2. CObeans.xml is actually the opposite of legacy - it is part of the still quite young CDI specification. The fact that you leave it empty is because usually you configure stuff through annotations and not through XML configuration anymore; the same for the web.xml which IS pretty much a legacy thing.
      singulars
    3. COThanks Gimby, that's useful. @Merlin those differences are pretty obvious, that's not the problem. It's not like the files have a handy description saying "This is a Web Server" file, "This is a Spring Framework file", etc. In fact most of my question is trying to to find out what the connection between the files and the various components of the system is. (I only mentioned Netbeans because Netbeans created the project with these files present so they might have been different to if a different IDE creates them).
      singulars
 

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