How does JSF find beans annotated with @ManagedBean?
Asked Answered
H

2

19

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

Hook answered 15/9, 2010 at 12:3 Comment(1)
Just switched to jsf from spring to see how is it. Found out it is a bit crazy. Since you have been working on it long time ago. Did you find it better?Allout
C
25

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 Reflections for this instead of homegrowing it.

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

In a Java EE environment, better yet is to use CDI instead.


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?

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

See also:

Corroborate answered 15/9, 2010 at 12:53 Comment(2)
@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.Corroborate
great answer! thank you a lot! (the answer for my question was adding faces-config file to the dependency .jar file)Goalie
C
4

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.

Coquet answered 15/9, 2010 at 12:15 Comment(4)
Isn't it possible to specify a single JAR file to scan too?Hook
How to specify where to look ?Pedometer
@org: He's confusing faces-context.xml with faces-config.xml. I think you already know how to declare beans in faces-config.xml? :)Corroborate
@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?Pedometer

© 2022 - 2024 — McMap. All rights reserved.