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:
Thanks in advance,
Tim
web.xml is a file that should reside in all J2EE web application. Its specification is defined by J2EE spec. Here you configure a general behaviour of your app. For example servlets, filters, security policy etc.
dispatcher-servlet is a special servlet in Spring MVC framework. You must define its mapping in your web.xml to enable Spring in your web app.
beans.xml is a file that is use for configure some CDI settings. For example bean-discovery-mode="annotated"
means that only classes annoteded with CDI scope annotation will be consider as CDI managed beans.
applicationContext.xml here you are actually right. It is the common name of the main Spring configuration file. You can set many things here like for example create and wire some Spring beans.
glassfish-web.xml is generally an extension to web.xml for GlassFish server. It is not always needed. You need it if want to configure some settings specially for GlassFish server. For example if you configure the security part in your web.xml you have to map user roles from web.xml to GlassFish realm roles.
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With