Where should beans.xml be placed?

wobblycogs picture wobblycogs · Jan 24, 2012 · Viewed 22.9k times · Source

I've just upgraded to NetBeans 7.1 from 7.0. On opening a JSF managed bean it "helpfully" told me that it couldn't find my beans.xml file so would I like it created for me. I knew I had a beans.xml file under WEB-INF but I said yes anyway to see what happened. A new beans.xml was created under META-INF for me.

Thinking I'd made a mistake I deleted the file under WEB-INF only to have my application fail at start up. Putting beans.xml back into WEB-INF fixed that problem. This page seems to think both locations are valid: http://seamframework.org/Documentation/WhatIsBeansxmlAndWhyDoINeedIt

So, the question is which folder should beans.xml live in WEB-INF or META-INF?

I'm running GlassFish 3.1.1 and Java 7

Answer

Mikko Maunu picture Mikko Maunu · Feb 12, 2012

Having beans.xml placed to the WEB-INF directory is fine in your case, because most likely beans are under WEB-INF classes.

Correct place for beans.xml depends about type of archive and location of bean classes.

In specification this is explained as follows:

Bean classes of enabled beans must be deployed in bean archives.

  • A library jar, EJB jar, application client jar or rar archive is a bean archive if it has a file named beans.xml in the META-INF directory.
  • The WEB-INF/classes directory of a war is a bean archive if there is a file named beans.xml in the WEB-INF directory of the war.
  • A directory in the JVM classpath is a bean archive if it has a file named beans.xml in the META-INF directory.