gpt4 book ai didi

java - 无法解析类型 : xxx; Qualifiers: [@javax. enterprise.inject.Any()] 的任何 bean]

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:11 26 4
gpt4 key购买 nike

我有一个 LoginProvider 接口(interface):

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

还有 2 种不同的实现方式:

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

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

@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()]

谢谢!

最佳答案

我在 LoginProvider1Impl 和 LoginProvider2Impl 中添加了注解 @ApplicationScoped
另一种使其工作的方法是修改工厂方法:

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

关于java - 无法解析类型 : xxx; Qualifiers: [@javax. enterprise.inject.Any()] 的任何 bean],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31812173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com