可以将@ManagedBean和@XxxScope放在基类中吗? [英] Can @ManagedBean and @XxxScope be placed in a base class?

查看:136
本文介绍了可以将@ManagedBean和@XxxScope放在基类中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个@ManagedBean(javax.faces.bean.ManagedBean),父级和子级.父级托管bean不是抽象的,因为我们必须给予开发人员自由,以便在足够的情况下使用父级,或者将其继承给具有特殊功能的子级.

I have two @ManagedBean (javax.faces.bean.ManagedBean), parent and child. The parent managed bean is not abstract because we have to give liberty to the developer to use the parent if enough or inherit it with a child that holds specifically funcionality.

在注入bean和父bean中带有@PostConstruct注释的方法时,我遇到了问题.

I have problems with the injections bean and the @PostConstruct annotated method in the parent bean.

下面的代码是我发现它起作用的唯一方法.

The following code is the only way I found it works.

@ManagedBean(name = "myBean")
@SessionScoped
public class BaseBean implements Serializable {
    @ManagedProperty(value = "#{serviceManagerController}")
    protected ServiceManagerController serviceManagerController;

     @PostConstruct
     public void init() {
       //do things
     }
}

还有子豆

public class ChildBean extends BaseBean {
    @PostConstruct
    public void init() {
       super.init();
    }
}

要覆盖"myBean" bean并强制应用程序在需要时使用子bean,我必须在faces-config.xml

To override the "myBean" bean and force to the app to use the child bean when needed I have had to declare the 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 is the only way all works and I don´t understand some things.

  1. 如果我未在faces-config.xml中声明子bean,那么虽然继承了@ManagedBean,但是bean容器始终使用父bean实现.
  2. 除非在faces-config.xml子bean声明中声明<managed-property>,否则不会执行像serviceManagerController这样的对父bean的注入.
  3. @PostConstruct方法不在父子级中执行,仅在@PostConstruct子级中执行.因此,我必须在子bean中的空@PostConstruct方法中调用super.init()
  1. If I don´t declare the child bean in faces-config.xml then the beans container always uses the parent bean implementation though @ManagedBean is inherited.
  2. The injections in parent bean, like serviceManagerController are not performed unless I declare the <managed-property> in the faces-config.xml child bean declaration.
  3. The @PostConstruct method is not executed in the parent child, just the @PostConstruct child. Because of that I have to call super.init() in an empty @PostConstruct mehtod in the child bean

为什么必须执行这三个步骤才能在父级工作中进行注入和postConstruct?

Why do I have to do this three steps to make injections and postConstruct in the parent work?

当然,如果在我的应用程序中,我不想继承BaseBean并想在facelets中使用此bean,那么所有操作都不会出现问题.

Of course, if in my app I don´t want to inherit BaseBean and want to use this bean in the facelets all work without problems.

致谢

推荐答案

BaseBean设计错误. Bean管理注释是继承的.从技术上讲,在相同的受管bean名称/标识符上注册不同子类的多个实例是没有任何意义的. BaseBean类必须为abstract,并且没有任何bean管理注释(这样,您和JSF都无法意外地"实例化它).将这些bean管理放到ChildBean上.您的faces-config.xml修复"基本上可以做到这一点.

The BaseBean is wrongly designed. Bean management annotations are not inherited. It does technically not make any sense to have multiple instances of different subclasses registered on the very same managed bean name/identifier. The BaseBean class must be abstract and not have any bean management annotations (so that neither you nor JSF can "accidentally" instantiate it). Put those bean management on ChildBean instead. Your faces-config.xml "fix" does basically exactly 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 and post construct / pre destroy annotations are however inherited, provided that you didn't already override them in the subclass. So you don't need to redefine them.

这篇关于可以将@ManagedBean和@XxxScope放在基类中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆