获取具有其他名称的会话范围 bean 的新实例

Get new instance of session scoped bean with other name

我有一个会话范围的 bean,供 UI 编辑一些数据。它用@Named 和@SessionScoped 注释,并且都在JBoss 6.2 中运行。现在我得到了几乎相似的编辑 UI 的要求。问题是两个 UI 可以并行存在。因此,为了完美重用,最好用另一个名称创建一个新的 bean 实例。不幸的是,我不知道如何以干净的 CDI 方式执行此操作。 我不太喜欢从bean继承并给另一个名字。这是我的想法之一。 另一个想法是只在托管 bean 中实现业务逻辑,并保持从它们封装的数据,并在特定上下文需要时将数据对象设置在托管 bean 中。但也许还有另一种 CDI 方式与制作人之类的? 在我的例子中,将 bean 的范围更改为 ViewScope 没有任何意义。

谢谢 奥利弗

But maybe there is another CDI way with producers or something

确实,您可以使用生产者。

开球示例:

@SessionScoped
public class SessionBean {

    @Produces
    @Named("foo")
    @SessionScoped
    public SessionBean getAsFoo() {
        return new SessionBean();
    }

    @Produces
    @Named("bar")
    @SessionScoped
    public SessionBean getAsBar() {
        return new SessionBean();
    }

    // ...
}

(方法名随意)

用法:

@Inject
@Named("foo")
private SessionBean foo;
@Inject
@Named("bar")
private SessionBean bar;