Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject @EJB bean based on conditions

Tags:

jakarta-ee

ejb

A newbie question: is there anyway that I can inject different beans based on a condition that I set in a properties file. Here's what I want to achieve:

I set some value in properties file. If it's true, then I want to

  public class MyClass{
    @EJB
    private MyBean bean;
  }

if it's false, then

public class MyClass{
  @EJB
  private MyBean2 bean2;
 }

Is this doable?

like image 649
neo Avatar asked Oct 27 '11 17:10

neo


People also ask

What is the difference between @EJB and @inject?

The @Inject version will respect the scope of the EJB. For example, using @EJB to inject an SFSB into a servlet makes no sense because only one SFSB will exist for every request. Using @Inject to inject a @SessionScoped SFSB into a servlet means you have a CDI proxy that creates a new SFSB as needed for each session.

How does EJB dependency injection work?

EJB 3.0 specification provides annotations, which can be applied on fields or setter methods to inject dependencies. EJB Container uses the global JNDI registry to locate the dependency. Following annotations are used in EJB 3.0 for dependency injection.

Where is it possible to use dependency injection annotation in EJB 3. 0?

The @EJB annotation only injects EJB stubs. A more generic dependency injection annotation is @Resource . Using the @Resource annotation, you can inject any service object from the JNDI using the object's JNDI name. Both global (java:/) and local (java:comp/env) JNDI trees are searched.

What is the use of @EJB annotation?

Annotations were introduced in Java 5.0. The purpose of having annotations is to attach additional information in the class or a meta-data of a class within its source code. In EJB 3.0, annotations are used to describe configuration meta-data in EJB classes.


2 Answers

As Gonzalo said, you would firstly need to specify the common interface of the bean if you want to declare it as a class field and use different implementations of it.

Moreover, I think you could achieve it more elegant using the CDI's @Produces method; i.e. somewhat between these lines:

@Singleton
@Startup
public class Configuration {

    private boolean someCondition;

    @PostConstruct
    private void init() {
        someCondition = ... // get a value from DB, JMS, XML, etc.
    } 

    @EJB(lookup="java:comp/env/myParticularBean")
    MyBean myBean1;

    @EJB(beanName="anotherTypeOfBeanInjectedByName")
    MyBean myBean2;

    @Produces
    public MyBean produceMyBean() {
        if (someCondition)
            return myBean1;
        } else {
            return myBean2;
        }
    }
}

Then in your code you can just use:

@Inject
MyBean myBean;

and appropriate bean based on your condition will be injected for you.

If you don't need a field on class level you could use the old-way and locate the EJB in JNDI - in this way you have the control over what type and what bean should be located and used.

EDIT: I've added the @EJB annotated beans to show where the 'myBean1' and 'myBean2' instances might come from.

This example shows that you can have one, single place where you define all your dependencies on different EJB implementations and other components. In an examle, this could be realised as a singleton EJB with @EJB fields, @PersistenceContext fields, etc.

Instead of doing it in the presented way, you can change return myBean1 to something like return context.lookup("JNDI_NAMESPACE_COORDINATES") where context is an instance of InitialContext.

Hope this makes it more clear.

like image 77
Piotr Nowicki Avatar answered Sep 20 '22 14:09

Piotr Nowicki


I don't think you can modify the type of the bean being injected. I would say this is a Java restriction as it is a strongly typed language :)

You can however have the scenario where multiple beans implement the same interface, and you want to inject a specific implementation of that interface, as follows:

@Local
public interface MyBean {
}

@Stateless
public class MyBeanImpl1 implements MyBean {
}

@Stateless
public class MyBeanImpl2 implements MyBean {
}

Then you could do:

public MyClass {

@EJB(beanName="MyBeanImpl1")
MyBean myBean;

}

or

public MyClass {

@EJB(beanName="MyBeanImpl2")
MyBean myBean;

}

Depending on the implementation you want to inject.

like image 42
Gonzalo Garcia Lasurtegui Avatar answered Sep 19 '22 14:09

Gonzalo Garcia Lasurtegui