作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 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/
我是一名优秀的程序员,十分优秀!