org.jboss.weld.exceptions.UnsatisfiedResolutionException WELD-001334带有限定符的MyInterface类型的不满意依赖@Any @MyAnnotation

问题描述 投票:0回答:2

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

@EJB
public MyInterface delegate;

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

<?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的实现,所以在我的类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();
}

这是我的文字注释类的代码:

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带有限定符的MyInterface类型的不满意依赖@Any @MyAnnotation

有谁可以帮助我吗 ?

java jboss annotations cdi javabeans
2个回答
0
投票

似乎发生异常是因为CDI没有找到使用@Any和@MyAnnotation注释的bean,因为您想要选择的bean仅使用@MyAnnotation进行注释。我认为在实例注入时不应该使用@Any,因为Instance将能够访问MyInterface的所有实现,从而选择使用@MyAnnotation(MyEnum.value)注释的实现。我从未使用带有限定符的Instance,它使用枚举。

我认为你不应该使用Instance。以下注射不起作用吗?

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>    

这对你有帮助吗?


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.