arjacsoh
arjacsoh

Reputation: 9232

Dependency Injection of EJB

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?

Upvotes: 0

Views: 519

Answers (1)

Petr Mensik
Petr Mensik

Reputation: 27496

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 
}

Upvotes: 2

Related Questions