Unable to resolve any beans for Type: xxx; Qualifiers: [@javax.enterprise.inject.Any()]

半城伤御伤魂 提交于 2020-01-04 02:57:11

问题


I have a LoginProvider interface:

public interface LoginProvider {
    boolean login(String username, String password);
}

And 2 different implementations:

public class LoginProvider1Impl implements LoginProvider {
    @Override
    public boolean login(String username, String password) {
        ...
    }
}

public class LoginProvider2Impl implements LoginProvider {
    @Override
    public boolean login(String username, String password) {
        ...
    }
}

Then a producer annotation:

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, PARAMETER, TYPE, CONSTRUCTOR})
    public @interface LoginProviderProducer {
}

An annotation to specify the Login Provider implementation:

@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, TYPE})
public @interface LoginProviderType {
    LoginProviderName value();

    public enum LoginProviderName {

        PROVIDER1(LoginProvider1Impl.class),
        PROVIDER2(LoginProvider2Impl.class);

        private Class<? extends LoginProvider> loginProviderType;

        private LoginProviderName(Class<? extends LoginProvider> loginProviderType) {
            this.loginProviderType = loginProviderType;
        }

        public Class<? extends LoginProvider> getLoginProviderType() {
            return loginProviderType;
        }
    }
}

And a factory:

@ApplicationScoped
public class LoginProviderFactory {

    @Produces
    @LoginProviderProducer
    public LoginProvider createLoginProvider(@Any Instance<LoginProvider> instance, InjectionPoint injectionPoint) {
        Annotated annotated = injectionPoint.getAnnotated();
        LoginProviderType loginProviderTypeAnnotation = annotated.getAnnotation(LoginProviderType.class);
        Class<? extends LoginProvider> loginProviderType = loginProviderTypeAnnotation.value().getLoginProviderType();
        return instance.select(loginProviderType).get();
    }
}

Finally I have a helper in which the login providers are injected:

@ApplicationScoped
public class LoginProviderHelperImpl implements LoginProviderHelper {

    @Inject
    @LoginProviderProducer
    @LoginProviderType(LoginProviderName.PROVIDER1)
    private LoginProvider provider1;

    @Inject
    @LoginProviderProducer
    @LoginProviderType(LoginProviderName.PROVIDER2)
    private LoginProvider provider2;

    ...
}

I get this error when the helper is used:

org.jboss.weld.exceptions.UnsatisfiedResolutionException:
WELD-001308: Unable to resolve any beans for Type:
class com.xxx.LoginProvider1Impl; Qualifiers: [@javax.enterprise.inject.Any()]

Thanks!


回答1:


I added the annotation @ApplicationScoped to LoginProvider1Impl and LoginProvider2Impl.
Another way to make it work is to modify the factory method:

@ApplicationScoped
public class LoginProviderFactory {

    @Produces
    @LoginProviderProducer
    public LoginProvider createLoginProvider(@Any Instance<LoginProvider> instance, InjectionPoint injectionPoint) {
        Annotated annotated = injectionPoint.getAnnotated();
        LoginProviderType loginProviderTypeAnnotation = annotated.getAnnotation(LoginProviderType.class);
        Class<? extends LoginProvider> loginProviderType = loginProviderTypeAnnotation.value().getLoginProviderType();
        //return instance.select(loginProviderType).get();
        if (loginProviderType == LoginProvider1Impl.class) {
            return new LoginProvider1Impl();
        } else if (loginProviderType == LoginProvider2Impl.class) {
            return new LoginProvider2Impl();
        } else
            return null;
    }
}


来源:https://stackoverflow.com/questions/31812173/unable-to-resolve-any-beans-for-type-xxx-qualifiers-javax-enterprise-inject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!