Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2 - @ManagedBean not detected

I have a ParentBean class which has the @ManagedBean annotation. If I subclass this class, the ChildBean does not inherit the @ManagedBean annotation although the @ManagedBean annotation has the meta-annotation Inherited.

What am I missing ?

JSF 2.0
Mojarra 2.0.6 | JBoss 5.1.0.GA
Eclipse Indigo - Build id: 20110615-0604

EDIT: Still looking for a solution ...

like image 418
Stephan Avatar asked Jul 12 '11 08:07

Stephan


2 Answers

javax.annotation.ManagedBean does not have @Inherited. javax.faces.ManagedBean has. Check your imports.

Furthermore, I wouldn't rely on that. I don't see it defined in the spec, but usually inheritance with jsf managed bean is a bad idea. Even if you have it, you should have an abstract base class that holds common functionality but is not a managed bean by itself.

Another thing - reading the code later on would be harder if you don't have the annotation on the bean. It may take a while until the reader realizes it is inherited.

So put @ManagedBean on each bean, and don't overuse inheritance.

like image 148
Bozho Avatar answered Nov 06 '22 10:11

Bozho


After reading this question and reading a little bit the source code from Mojarra (see question), I think Mojarra may not actually scan thoroughly the classes (for loading performance).

So my guess is that for one given class, the Mojarra AnnotationScanner stays on this class and it doesn't scan the hierarchy of this same class.

This is why my ChildBean is not recognized as a ManagedBean.

like image 4
Stephan Avatar answered Nov 06 '22 11:11

Stephan