Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JSF find beans annotated with @ManagedBean?

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

like image 725
Iravanchi Avatar asked Sep 15 '10 12:09

Iravanchi


People also ask

What is ManagedBean in JSF?

Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a Java bean managed by JSF framework. Managed bean contains the getter and setter methods, business logic, or even a backing bean (a bean contains all the HTML form value). Managed beans works as Model for UI component.

What does JSF use to locate the beans that are managed within the JSF application?

This is an alternative to the application configuration resource file approach and reduce the task of configuring managed beans. The @RequestScoped annotation is used to provide scope for ManagedBean. You can use annotations to define the scope in which the bean will be stored.

What is the use of @ManagedBean?

Managed beans are container-managed objects with minimal supported services, such as resource injection, life cycle callbacks and interceptors, and have the following characteristics: A managed bean does not have its own component-scoped java:comp namespace.

What is the difference between managed bean and backing bean in JSF?

1) BB: A backing bean is any bean that is referenced by a form. MB: A managed bean is a backing bean that has been registered with JSF (in faces-config. xml) and it automatically created (and optionally initialized) by JSF when it is needed.


1 Answers

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:

  • Structure for multiple JSF projects with shared code
like image 68
BalusC Avatar answered Oct 06 '22 00:10

BalusC