我使用weld-se
来测试我的JSR-330
注释类之一。
@Inject HelloWorld any;
@Inject @Named("impl") HelloWorld namedAsImpl;
@Inject @Named("demo") HelloWorld namedAsDemo;
@Inject @Impl HelloWorld qualifiedWithImpl;
@Inject @Demo HelloWorld qualifiedWithDemo;
我成功地用Guice,Dagger和Dagger2注入了这些字段。
我准备了一个提供者类。
@RequestScoped
public class HelloWorldWeldProvider {
@Produces HelloWorld any() {
return current().nextBoolean()
? new HelloWorldImpl() : new HelloWorldDemo();
}
@Produces @Named("impl") HelloWorld namedAsImpl() {
return new HelloWorldImpl();
}
@Produces @Named("demo") HelloWorld namedAsDemo() {
return new HelloWorldDemo();
}
@Produces @Impl HelloWorld qualifiedWithImpl() {
return new HelloWorldImpl();
}
@Produces @Demo HelloWorld qualifiedWithDemo() {
return new HelloWorldDemo();
}
}
Weld抱怨
WELD-001409: Ambiguous dependencies for type HelloWorld with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject ....any
at ....any(HelloWorldDependencyInjectionTest.java:0)
Possible dependencies:
- Producer Method [HelloWorld] with qualifiers [@Default @Any @Named] declared as [[BackedAnnotatedMethod] @Produces @Named ....namedAsDemo()],
- Producer Method [HelloWorld] with qualifiers [@Default @Any @Named] declared as [[BackedAnnotatedMethod] @Produces @Named ....namedAsImpl()],
- Producer Method [HelloWorld] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces ....any()]
这是正常的吗?或CDI
的工作方式与DI
不同?