views:

172

answers:

2

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...

+2  A: 

Actually, the container scans all classes within the current WAR file. Scanning all the classpath would be very long and costly.

That's why you can put your annotated managed beans within the WAR file, and not in a JAR file.

If you really need to put your classes in a JAR file in the classpath, you can use the faces-config.xml file to tell JSF which classes are managed bean.

Vivien Barousse
Isn't it possible to specify a single JAR file to scan too?
Iravanchi
How to specify where to look ?
org.life.java
@org: He's confusing `faces-context.xml` with `faces-config.xml`. I think you already know how to declare beans in `faces-config.xml`? :)
BalusC
@BalusC, where to specify that , where to look for. I don't think it will skip classpath , he says that it will look only in war, i don't think that jsf context won't look for class path. what do you say balusC?
org.life.java
@BaluC : Yes, I meant `faces-config.xml`. I typed a little too quickly...
Vivien Barousse
+2  A: 

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?

Start by peeking around in com.sun.faces.application.annotation.AnnotationManager in 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 Google Reflections for this instead of homegrowing it.

Set<Class<?>> classes = reflections.getTypesAnnotatedWith(SomeAnnotation.class);

Update to clarify: to have JSF to load any annotated managed beans from a JAR file, you have to put a /META-INF/faces-config.xml file 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.xml file 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.xml file can look like:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">
</faces-config>

Store it in the META-INF folder of the JAR.

This is by the way described in chapter 11.4.2 of JSF 2.0 specification.

11.4.2 Application Startup Behavior

...

This algorithm provides considerable flexibility for developers that are assembling the components of a JSF-based web application. For example, an application might include one or more custom UIComponent implementations, along with associated Renderers, so it can declare them in an application resource named “/WEB-INF/faces-config.xml” with no need to programmatically register them with Application instance. In addition, the application might choose to include a component library (packaged as a JAR file) that includes a “META-INF/faces-config.xml” resource. The existence of this resource causes components, renderers, and other JSF implementation classes that are stored in this library JAR file to be automatically registered, with no action required by the application.

BalusC
@BalucS I got the answer
org.life.java
@org: it's B, a, l, u, s and C :) By the way, `@nickname` is not needed whenever you want to reply on the owner of the post. The owner of the post will always be notified of any comments.
BalusC
ok BalusC [without @ ] :p
org.life.java
Thanks for the clear answer.
Iravanchi
You're welcome.
BalusC