简体   繁体   中英

CDI Injection into existing object instance - worked with CDI 1.0 but not with CDI 1.1

Due to limitations of a framework I have to use, I need to inject into existing object instances (can't created them as usual via CDI itself). My injection points are marked either with @EJB or @Inject .

With JBoss EAP 6.4, Java EE 6 and CDI 1.0 this worked perfectly fine with the following code:

public class DispatcherUtils {

    public static <T> void inject(T anObject) {
        BeanManager beanManager = getBeanManager();
        Class<T> objClass = (Class<T>) anObject.getClass();
        AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(objClass);
        InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
        CreationalContext<T> context = new IgnorantCreationalContext<>();
        injectionTarget.inject(anObject, context);
        injectionTarget.postConstruct(anObject);
    }

    private static BeanManager getBeanManager() {
        try {
            return (BeanManager) InitialContext.doLookup("java:comp/BeanManager");
        } catch (final NamingException e) {
            e.printStackTrace();
        }
        return null;
    }

}

If I try to do the same in JBoss EAP 7.0, Java EE 7 and CDI 1.1 only injection points marked with @Inject are injected into the target objects, those marked with @EJB are not injected (their value remains null).

I do not understand why that is.

Is there any way to inject into existing objects with JBoss EAP 7.0, Java EE 7 and CDI 1.1 and have @EJB injection points populated as well?


Update, 2016-06-19, 20:11

I just discovered that the above pattern works as long as the object I want to inject into has a no-arg constructor.

Example - this works :

public class ExampleBean {

    private Dispatcher dispatcher;

    @Inject
    private SomeCdiBean someCdiBean;

    @EJB
    private SomeEjbService someEjbService;

    public ExampleBean() {
    }

    public ExampleBean(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

}

while this does not work :

public class ExampleBean {

    private Dispatcher dispatcher;

    @Inject
    private SomeCdiBean someCdiBean;

    @EJB
    private SomeEjbService someEjbService;

    public ExampleBean(Dispatcher dispatcher) {
        this.dispatcher = dispatcher;
    }

}

Why does CDI check for the presence of a default constructor in such a case? (Note: the bean is not created by CDI, so the constructor shouldn't make any difference)

Possible problem might be that your ExampleBean without no-args constructor is not considered a managed bean . And an integrator (Wildfly, EAP, glassfish,...) can only provide EE resource injection ( @EJB being EE resource injection) into managed beans .

Quoting from Weld doc :

An integrator may wish to use InjectionServices to provide additional field or method injection over-and-above that provided by Weld. An integration into a Java EE environment may use InjectionServices to provide EE resource injection for managed beans.

To make ExampleBean a managed bean and not use no-args constructor, you need to declare a constructor annotated with @Inject .

Though I am just taking a wild guess here and cannot really be sure why you say it worked with EAP 6 and CDI 1.0.

And one more note just for the record - from my knowledge EAP 7 contains CDI 1.2 (rather than 1.1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM