Search code examples
springcdijava-ee-7

Get a CDI bean dynamically, NOT by injection


Inside of my method I want to get a CDI bean, but I don't want to use @Inject as I don't need this bean to be saved on instance level. I just want to get the instance and pass it to some other method.

Previously I was working with Spring, but now switch to JEE application. In Spring inside of my method I could simply write:

MyClass myClass = applicationContext.getBean(MyClass.class)

Is there a way to do something like this in JEE application?


Solution

  • I found the answer here: How to programmatically inject a Java CDI managed bean into a local variable in a (static) method

    Need to use:

    javax.enterprise.inject.spi.CDI.current().select(C.class).get()