opfau
opfau

Reputation: 761

Get new instance of session scoped bean with other name

I have a session scoped bean for a UI to edit some data. It is annotated with @Named and @SessionScoped and all runs in JBoss 6.2. Now I got the requirement for a nearly similar edit UI. The problem is that the two UIs can exist in parallel. So for a perfect reuse it would be nice to create a new instance of the bean with another name. Unfortunately I have no clue how to do this in a clean CDI way. I don't like it so much to inherit from the bean and give another name. This was one of my ideas. Another idea was to implement in the managed bean only the business logic and keep the data encapsulated from them and set the data object inside the managed bean when it is needed in the specific context. But maybe there is another CDI way with producers or something? Changing the scope of the bean to ViewScope makes no sense in my case.

Thanks Oliver

Upvotes: 2

Views: 722

Answers (1)

BalusC
BalusC

Reputation: 1108632

But maybe there is another CDI way with producers or something

Indeed, you could use a producer.

Kickoff example:

@SessionScoped
public class SessionBean {

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

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

    // ...
}

(method names are free to your choice)

Usage:

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

Upvotes: 2

Related Questions