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.
- if don´t declare child bean in
faces-config.xml
beans container uses parent bean implementation though@managedbean
inherited. - the injections in parent bean,
servicemanagercontroller
not performed unless declare<managed-property>
infaces-config.xml
child bean declaration. - the
@postconstruct
method not executed in parent child,@postconstruct
child. because of have callsuper.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.
Comments
Post a Comment