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.
I've not been able to find any clear description of this on Google and I've tried a few times.
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.
web.xml
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.
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
You add that in web.xml to get Spring Security working - but then you actually configure Spring Security in another file.
dispatcher-servlet
This seems to be similar to web.xml 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 viewResolver
. What should go in here and how does it differ from web.xml?
beans.xml
At the moment this file is empty apart from an xml root tag <beans>
and a few namespace/schema definitions in all my projects. Is it actually needed for anything?
Is bean-discovery-mode="annotated">
in the root tag the reason it is empty?
applicationContext
This seems to be where all the actual Spring configuration goes such as <mvc:annotation-driven />
, <context:component-scan />
etc.
You can also split this configuration up into multiple files and use <import />
to link those files into the application context.
glassfish-web
I've mostly been ignoring this file, is there any reason I shouldn't?
The questions
So really the questions are:
- What have I missed from the above?
- Why is there a separate beans.xml that seems to do nothing? Is it a legacy from before annotation driven was brought in?
- Why are there both dispatcher-servlet.xml and web.xml and what is the difference between them?
- How is glassfish-web.xml different from those two?
- 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 applicationContext.xml, installing spring components in web.xml, ignore the other files"!)
Thanks in advance,
Tim