27

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

albusshin
  • 3,930
  • 3
  • 29
  • 57
Tim B
  • 40,716
  • 16
  • 83
  • 128
  • 1
    Hi, 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. – Merlin Dec 12 '13 at 10:04
  • 1
    beans.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. – Gimby Dec 12 '13 at 10:29
  • Thanks 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). – Tim B Dec 12 '13 at 10:47

1 Answers1

28

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.

Flying Dumpling
  • 1,294
  • 1
  • 11
  • 13
  • Thanks, it sounds like the understanding I've pulled together is mostly correct. So web-xml defines DispatcherServlet, that then loads in the dispatcher-servelet xml configuration for itself and fires up the rest of the Spring Framework - which then loads ApplicationContext and parses that for configuration? – Tim B Dec 12 '13 at 10:50
  • 1
    Exactly :) Also notice that Spring and CDI are different dependency injection containers and are not compatible with each other. So either use Spring or CDI. – Flying Dumpling Dec 12 '13 at 11:09
  • So in fact beans.xml really is doing nothing because I'm using spring annotation driven for dependency injection? In fact if I tried to use whatever annotations that is connected to it would break things? – Tim B Dec 12 '13 at 11:12
  • 1
    Yep, you don't need bean.xml if you are going to use Spring DI. In fact, assuming that you have an implementation of CDI spec in your app server (and you do, in GlassFish it is weld), if you add some of CDI annotation it can cause errors or at least some performance overhead. It depends on what annotation you will use. Generally it is not worth explaining, just stick to the one DI framework and you are good to go. – Flying Dumpling Dec 12 '13 at 11:19
  • @FlyingDumpling Can you pls elaborate on `applicationContext.xml`? – Santhosh Mar 12 '14 at 06:57
  • Starting Servlet API 3.0, the web.xml is not mandatory. You can use instead the [WebServlet annotation](https://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html). – elirandav Mar 08 '18 at 15:36