org.jboss.weld.exceptions.UnsatisfiedResolutionException WELD-001334 不满足类型 MyInterface 与限定符 @Any @MyAnnotation 的依赖关系

org.jboss.weld.exceptions.UnsatisfiedResolutionException WELD-001334 Unsatisfied dependencies for type MyInterface with qualifiers @Any @MyAnnotation

我有一个 Java 8 项目和一个 JBoss 7.1.0GA 服务器。 我有一个带有全局变量

的批处理 class
@EJB
public MyInterface delegate;

在我的 ejb 中定义为 DelegateClass 的一个实例-jar.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
    <module-name>moduleName</module-name>
<enterprise-beans>
    <session>
        <ejb-name>ejbName</ejb-name>
        <business-remote>MyInterface</business-remote>
        <ejb-class>DelegateClass</ejb-class>
        <session-type>Stateless</session-type>
    </session>
</enterprise-beans>

我有一些 MyInterface 的实现,所以在我的 class DelegateClass 中,我想包装 MyInterface 的一个实现并通过枚举常量设置它。 这里的代码:

@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class DelegateClass implements MyInterface {

@Inject
@Any
protected Instance<MyInterface> instance;

protected MyInterface selectedImpl;

@PostConstruct
public void init() {
    selectedImpl = instance.select(MyLiteralAnnotation.create(MyEnum.value)).get();
}

这里是我文字注释的代码class:

public class MyLiteralAnnotation extends AnnotationLiteral<MyAnnotation> implements MyAnnotation {

private final MyEnum value;

private MyLiteralAnnotation(MyEnum value) {
    this.value = value;
}

@Override
public MyEnum getValue() {
    return value;
}

public static MyLiteralAnnotation create(MyEnum value) {
    return new MyLiteralAnnotation(value);
}

}

我创建的注释:

@Retention(RetentionPolicy.RUNTIME)

@Target({ TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR }) 
@Qualifier 
public @interface MyAnnotation {

MyEnum getValue();
}

以及我希望从 instance.select(...)

获得的实现
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@MyAnnotation(MyEnum.value)
public class MyInterfaceImpl implements MyInterface {
....
}

我在我的服务器上调试应用程序,但是当我尝试用 instance.select(...) 实例化 selectImpl 字段时,出现异常:

org.jboss.weld.exceptions.UnsatisfiedResolutionException WELD-001334 Unsatisfied dependencies for type MyInterface with qualifiers @Any @MyAnnotation

有人可以帮我吗?

异常好像是因为CDI没有找到注解@Any和@MyAnnotation的bean,因为你要的select只注解了@MyAnnotation。我认为在 Instance 注入时不需要 @Any,因为 Instance 将能够访问 MyInterface 的所有实现,因此你 select 用 @MyAnnotation(MyEnum.value) 注释的那个。我从未将实例与使用枚举的限定符一起使用。

我认为您不需要使用 Instace。 下面的注入不行吗?

class MyDelegate implements MyInterface{

   @Inject
   @MyAnnotation(MyEnum.value)
   private MyInterface myInterface;
}

如果您需要委托给另一个实现,那么也许 CDI-Decorators 就是您所需要的。

@Decorator
@Default // We decorate the default implementation of MyInterface or any qualified 
         // one you need to decorate
abstract class MyDecorator implements MyInterface {

   @Inject
   @MyAnnotation(MyEnum.value)
   private MyInterface myInterface;

   @Inject
   @Delegate
   private MyInterface delegate;

   // Implement all methods you want to decroate

   public void myMethod(){
      if(condition) {
         myInterface.myMethod();
      } else {
         delegate.myMethod();
      }
   }
}

您需要在 beans.xml

中注册装饰器
<decorators>
    <class>MyDecorator</class>
</decorators>    

对你有帮助吗?

我在委托 class 定义上添加注释 @ApplicationScope 解决了我的问题。