marlon
marlon

Reputation: 7723

EJB bean or plain Managed Bean?

Is this an EJB or ManagedBean? To be an EJB bean, must it be annotated by @stateful, @stateless or @Singleton? I thought @SessionScoped and @ApplicationScoped classes are also EJB beans. Is this right?

import javax.faces.bean.SessionScoped;
import javax.inject.Named;

@Named("userData")
@SessionScoped
public class UserData implements Serializable {

private static final long serialVersionUID = 1L;

public UrlData data;

public UrlData getData() {
    return data;
}

public void setData(UrlData data) {
    this.data = data;
}    
}

Upvotes: 1

Views: 782

Answers (1)

wittakarn
wittakarn

Reputation: 3162

@SessionScoped and @ApplicationScoped are part of ManagedBean not EJB, which classes as a resource with the JavaServer Faces.

  • @ApplicationScoped: Application scope persists across all users’ interactions with a web application.
  • @SessionScoped: Session scope persists across multiple HTTP requests in a web application.
  • @ViewScoped: View scope persists during a user’s interaction with a single page (view) of a web application.
  • @RequestScoped: Request scope persists during a single HTTP request in a web application.

You can see more detail in Java-EE specification

This is an example code that inject EJB into ManagedBean

@ManagedBean
@SessionScoped
public class Count {
    @EJB
    private CounterBean counterBean;

    private int hitCount;

    public Count() {
        this.hitCount = 0;
    }

    public int getHitCount() {
        hitCount = counterBean.getHits();
        return hitCount;
    }
    public void setHitCount(int newHits) {
        this.hitCount = newHits;
    }
}

Upvotes: 1

Related Questions