Is my class a bean?

For almost every Java class that we as developers have ever written, the answer would be yes, most definitely, as long as the Java class has either a constructor with no parameters or a constructor that is annotated with @Inject.

There is only one requirement that CDI mandates for a Java class to be injected as a bean, and that's for the Java class to be packaged into an appropriate archive (such as a JAR or WAR) that contains a descriptor file called beans.xml. This descriptor file needs to be present in the META-INF folder of a JAR or the WEB-INF folder of a WAR.

It's perfectly fine for beans.xml to be completely empty or only contain the following XML content:

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...

Get JBoss Weld CDI for Java Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.