As far as I know, for using @Annotations (or [Attributes] in C#) you have to have a reference to the class metadata, so that you can ask if the class is annotated (attributed) or not.
My question is how does JSF implementation find all classes annotated with @ManagedBean? Does it scan all of the classes in the class path? Or is there a way to actually “query” the JVM for the annotated classes?
I’m asking this because when I put my annotated backing beans in my web project directly, there’s no problem. But the beans that I define in the JAR files (to be reusable across projects) are not registered. Is there something that I have to tell MyFaces to direct it which JAR files to look at?
Also, using annotations introduce many nice patterns of programming. I want to know if I can find all annotated classes somehow…
Start by peeking around in
com.sun.faces.application.annotation.AnnotationManagerin Mojarra sources. Note that this is not part of the API, but implementation-specific.If you intend to use such tools for your own projects, I recommend using Reflections for this instead of homegrowing it.
In a Java EE environment, better yet is to use CDI instead.
To have JSF to load any annotated managed beans from a JAR file, you have to put a
/META-INF/faces-config.xmlfile in the JAR file. Just a JSF 2.0 compatible<faces-config>declaration is sufficient to get the JSF scan the JAR file for any interesting annotated classes. If the/META-INF/faces-config.xmlfile is not present in the JAR file, then JSF won’t scan the JAR file to improve loading performance.Here’s how a minimum JSF 2.0 compatible
faces-config.xmlfile look like:Store it in the
META-INFfolder of the JAR.This is by the way described in chapter 11.4.2 of JSF 2.0 specification.
See also: