jsf - Can @ManagedBean and @XxxScope be placed in a base class? -


i have 2 @managedbean (javax.faces.bean.managedbean), parent , child. parent managed bean not abstract because have give liberty developer use parent if enough or inherit child holds funcionality.

i have problems injections bean , @postconstruct annotated method in parent bean.

the following code way found works.

@managedbean(name = "mybean") @sessionscoped public class basebean implements serializable {     @managedproperty(value = "#{servicemanagercontroller}")     protected servicemanagercontroller servicemanagercontroller;       @postconstruct      public void init() {        //do things      } } 

and child bean

public class childbean extends basebean {     @postconstruct     public void init() {        super.init();     } } 

to override "mybean" bean , force app use child bean when needed have had declare child bean in faces-config.xml

<managed-bean>     <managed-bean-name>mybean</managed-bean-name>     <managed-bean-class>package.childbean</managed-bean-class>     <managed-bean-scope>session</managed-bean-scope>     <managed-property>         <property-name>servicemanagercontroller</property-name>         <property-class>package.servicemanagercontroller</property-class>         <value>#{servicemanagercontroller}</value>     </managed-property> </managed-bean> 

that way works , don´t understand things.

  1. if don´t declare child bean in faces-config.xml beans container uses parent bean implementation though @managedbean inherited.
  2. the injections in parent bean, servicemanagercontroller not performed unless declare <managed-property> in faces-config.xml child bean declaration.
  3. the @postconstruct method not executed in parent child, @postconstruct child. because of have call super.init() in empty @postconstruct mehtod in child bean

why have 3 steps make injections , postconstruct in parent work?

of course, if in app don´t want inherit basebean , want use bean in facelets work without problems.

regards

the basebean wrongly designed. bean management annotations not inherited. technically not make sense have multiple instances of different subclasses registered on same managed bean name/identifier. basebean class must abstract , not have bean management annotations (so neither nor jsf can "accidentally" instantiate it). put bean management on childbean instead. faces-config.xml "fix" that.

public abstract class basebean implements serializable {      @managedproperty("#{servicemanagercontroller}")     protected servicemanagercontroller servicemanagercontroller;      @postconstruct     public void init() {         // ...     }      // ... } 
@managedbean("mybean") @sessionscoped public class childbean extends basebean {     // ... } 

managed property , post construct / pre destroy annotations inherited, provided didn't override them in subclass. don't need redefine them.

see also:


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -