CDI object not proxyable with injected constructor

前端 未结 5 1948
广开言路
广开言路 2021-02-20 01:07

When trying to inject arguments into the constructor of a CDI bean (ApplicationScoped), I\'m encountering the following issue:

Caused by: org.jboss.weld.exceptio         


        
5条回答
  •  礼貌的吻别
    2021-02-20 01:50

    I think Vladimir is pointing you to the right direction.

    I'm just learning CDI (and Weld) therefore my answer might not be 100% accurate in all aspects, but it seems like you need to pick a type at injection point that has a no arg constructor.

    I hit the same error today with the next type hierarchy:

    • there is a provided IServerInterceptor interface,
    • there is a custom implementation AuthenticationInterceptor that has one constructor that takes dependencies (fun fact: this knows nothing about (C)DI),
    • there is an InjectableAuthenticationInterceptor which has only one injected constructor.

    I have another bean where I would like to inject an instance of AuthenticationInterceptor. It works, when I define the field as type IServerInterceptor (as it is an interface and weld can create a proxy for it(?)), but it stops working when I define the field as AuthenticationInterceptor.

    With some code:

    // IServerInterceptor.java
    public interface IServerInterceptor {
    }
    
    // AuthenticationInterceptor.java
    public class AuthenticationInterceptor extends InterceptorAdapter {
        private final Predicate validAccessTokenString;
        private final Function toAccessTokenModel;
        private final LoginManager loginManager;
    
        public AuthenticationInterceptor(Predicate validAccessTokenString, Function toAccessTokenModel, LoginManager loginManager) {
            this.validAccessTokenString = validAccessTokenString;
            this.toAccessTokenModel = toAccessTokenModel;
            this.loginManager = loginManager;
        }
        // ...
    }
    
    
    // InjectableAuthenticationInterceptor.java
    @ApplicationScoped
    public class InjectableAuthenticationInterceptor extends AuthenticationInterceptor {
        @Inject
        public InjectableAuthenticationInterceptor(LoginManager loginManager) {
            super(isWelformedAccessToken(), toAccessToken(), loginManager);
        }
    }
    

    Now,

    @Inject private IServerInterceptor authenticationInterceptor;
    

    works great, but

    @Inject private AuthenticationInterceptor authenticationInterceptor;
    

    does not.

提交回复
热议问题