Question

Is it possible to @Inject a stateless session bean into a subclass of AuthenticatorBase?

I'm using JBoss as 7.1.1.

My code looks like this:

...

public class myValve extends AuthenticatorBase {

    @Inject AuthController controller;

    //some code ...

}

Using the controller object leads to NullPointerException.

Was it helpful?

Solution

If controller is null it means that the myValve object itself was not injected.

It is possible to add an existing object to the CDI context retroactively, for example with this code:

public <T> void addToCDI(T object) {
    BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager();
    AnnotatedType<T> annotatedType = beanManager.createAnnotatedType((Class<T>)bject.getClass());
    InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
    CreationalContext<T> context = beanManager.createCreationalContext(null);
    injectionTarget.inject(object, context);
}

After the execution of this code the injections have been performed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top