문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top