Search code examples
javadependency-injectioncdijboss-weldweld

How to programmatically inject a Java CDI managed bean into a local variable in a (static) method


How can I programmatically inject a Java CDI 1.1+ managed bean into a local variable in a static method?


Solution

  • To inject an instance of class C:

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

    This is available in CDI 1.1+