Sessionscoped producer field with injection and qualifier

Posted: February 11, 2013 in web

xhtml

...
<p:inputText id="username" value="#{userAccount.user.username}" />
...

BackingBean

...
    @Inject
    @Current
    UserAccount userAccount;


    public List<Account> getAccounts() {
        return service.getAccounts(userAccount.getUser().getUsername());
    }

...

UserAccount.java (pojo)

public class UserAccount implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 8145761802030787200L;

    private User user;

    public UserAccount() {
        user = new User();
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

UserAccountProducer.java (producer)

@SessionScoped
public class UserAccountProducer implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -8919383621377088330L;

    private UserAccount userAccount;

    @Produces
    @Current
    @Named
    public UserAccount getUserAccount() {
        if (userAccount == null) {
            userAccount = new UserAccount();
        }

        return userAccount;
    }

    public void setUserAccount(UserAccount userAccount) {
        this.userAccount = userAccount;
    }

}

Current.java qualifier


@Qualifier
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
public @interface Current {

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s