无法解析Type:xxx的任何bean;资格赛:[@ javax.enterprise.inject.Any()]

时间:2015-08-04 14:34:39

标签: java dependency-injection cdi weld

我有一个LoginProvider界面:

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

有两种不同的实现方式:

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) {
        ...
    }
}

然后是生产者注释:

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

用于指定登录提供程序实现的注释:

@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;
        }
    }
}

还有一家工厂:

@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();
    }
}

最后,我有一个帮助程序,其中注入了登录提供程序:

@ApplicationScoped
public class LoginProviderHelperImpl implements LoginProviderHelper {

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

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

    ...
}

使用帮助程序时出现此错误:

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

谢谢!

1 个答案:

答案 0 :(得分:1)

我将注释@ApplicationScoped添加到LoginProvider1Impl和LoginProvider2Impl。
使其工作的另一种方法是修改工厂方法:

@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;
    }
}