0

I am studying the Dependency Injection of EJB without using CDI and two questions have come up. I would be grateful to anyone could answer the following:

1) Is it possible to inject Session Beans (Stateful ot Stateless) with @EJB annotation within Message Driven Beans?

2) If one lets two Session Beans implement the same interface, can one inject them with @EJB annotation specifying only the interface name? How can one make the Container aware of the specific Bean class which is to be injected? For example:

@Remote
public interface RemoteInterface{}

@Stateless
public class BeanA implements RemoteInterfaceA{}

@Stateless
public class BeanB implements RemoteInterfaceA{}

@Stateful
public class StatefulBean{

@EJB
RemoteInterface

}

How can one specify which Bean is to be injected without using CDI and Qualifiers?

arjacsoh
  • 8,932
  • 28
  • 106
  • 166

1 Answers1

2

1)Of course, you usually want to call methods from some service EJB when you receive a message in MDB.

2)It's possible, you can name your bean and then inject it by that name, see my example

@Stateless(name="bean1")
public class BeanA implements RemoteInterfaceA{}

@Stateless(name="bean2")
public class BeanB implements RemoteInterfaceA{}

@Stateless
public class Bean3 {

    @EJB(beanName="bean1")
    private RemoteInterfaceA bean;  
    //first bean should get injected here 
}
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • I would like to add some clarification. Although it is possible to inject a Stateful bean within a MDB , this has not much sense. http://stackoverflow.com/questions/6527552/is-it-legal-to-inject-a-stateful-into-an-mdb – Gabriel Aramburu Jan 06 '14 at 13:39