2

In the top answer to this question for example : Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean I read that:

To deploy CDI beans, you must place a file called beans.xml in a META-INF folder on the classpath. Once you do this, then every bean in the package becomes a CDI bean.

And also it is said that:

If you want to use the CDI bean from a JSF page, you can give it a name using the javax.inject.Named annotation.

I have a sample code that goes like this:

@ManagedBean
@ViewScoped
public class SignUpPage {

    private User user;

    @PostConstruct
    public void init() {
        user = new User();
    }

    @Inject
    private UserDao userDao;
// rest of the class

So as far as I understand, my bean is still a JSF Managed Bean, it is not a CDI bean(or is it?). By the way, I have a beans.xml in my WEB-INF folder.

And @Inject works just fine in here. Also, I can access the bean with EL just fine(which makes me think it is still a JSF Managed Bean)

The UserDao class looks something like this:

@Stateless
public class UserDao {
    EntityManager em;
    @PostConstruct
    public void initialize(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Persistence");
        em = emf.createEntityManager();
    }

So, it is as far as I know an EJB..

So do I have any CDI beans in this example? How does @Inject work here?

Hope my question is clear, Regards!

Community
  • 1
  • 1
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

1 Answers1

2

By CDI specification, every JavaBean is a Managed Bean (do not confuse it with JSF @ManagedBean, this is a different one) in project where the beans.xml is present. So every class is also eligible for dependency injection. Note that default scope of this class is Dependent.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • So for example the Dao class here is an EJB + a CDI bean. And SignUpPage is a JSF Managed Bean + a CDI bean? – Koray Tugay Sep 02 '13 at 20:01
  • yes, but is a really bad practise to mix up CDI with JSF, you can get unexpected results. So make use `@Named` instead of `@ManagedBean` and make sure that your scopes are from the package `javax.enterprise.context.*` – Petr Mensik Sep 02 '13 at 20:05
  • How about mixing CDI with EJB? If I change the SignUpPage to a CDI bean only ( with Named and from that package you mention ), is it ok to use Inject annotation? If I use the EJB annotation, is it ok as well? – Koray Tugay Sep 02 '13 at 20:08
  • You can have even have CDI, EJB and JSF bean in once:-)And for the first question - yes, you can use `@Inject` for EJB beans, it's even a good practise. You only need `@EJB` if you are injection remote EJBs – Petr Mensik Sep 02 '13 at 20:12
  • Interesting, thanks I had no idea. So I can have a Statless ManagedBean which is Sessionscoped? That is confusing.. – Koray Tugay Sep 02 '13 at 20:14
  • I suppose you can but it really doesn't make sense. So you really need to mix up CDI and EJB reasonably:) – Petr Mensik Sep 05 '13 at 21:56