15

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?

neo
  • 2,461
  • 9
  • 42
  • 67
  • Performing a JNDI lookup would be viable alternative yielding the same postcondition, but migrating your code between Application Servers would be difficult as the default JNDI path to EJBs is not standardised. – 8bitjunkie Jul 26 '13 at 15:03

2 Answers2

28

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;

An 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 example, 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.

homaxto
  • 5,428
  • 8
  • 37
  • 53
Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
  • thanks for the suggestion. but i dont quite understand how to return "myBean1" or "myBean2" when the return type is "MyBean"? Can you show me a bit more details? – neo Oct 31 '11 at 19:05
  • myBean1 and myBean2 are instances of classes which implements MyBean. I'll update the question to show some more code – Piotr Nowicki Oct 31 '11 at 19:16
  • Thanks PedroKowalski. so which class should have method "public MyBean produceMyBean()" ? – neo Nov 01 '11 at 14:27
  • @neo whatever suits you :-) It could be an Singleton EJB if you want to use `@EJB` as shown in the example. It could be a regular class if you prefer to use JNDI lookup manually. – Piotr Nowicki Nov 01 '11 at 15:01
  • Thanks and take a look at the example - hope this time it's clearer. – Piotr Nowicki Nov 01 '11 at 17:15
  • I tried this and defining a `@Produces` method for an EJB interface and having defined an `@EJB` implementing the same interface creates a ambiguous dependency problem for the container. – PålOliver Apr 13 '15 at 07:55
  • This solution requires the definition of a [javax.inject.Qualifier](https://docs.oracle.com/javaee/7/api/javax/inject/Qualifier.html) in order to work correctly – Steve C Jan 29 '19 at 03:11
2

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.