Inject @EJB bean based on conditions

前端 未结 2 1488
面向向阳花
面向向阳花 2020-12-03 04:06

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

2条回答
  •  心在旅途
    2020-12-03 04:11

    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.

提交回复
热议问题