java - org.jboss.weld.exceptions.UnsatisfiedResolutionException WELD-001334 带有限定符 @Any @MyAnnotation 的类型 MyInterface 的依赖关系不满足
问题描述
我有一个 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 带有限定符 @Any @MyAnnotation 的类型 MyInterface 的依赖关系不满足
有人能帮助我吗 ?
解决方案
出现异常似乎是因为 CDI 没有找到使用 @Any 和 @MyAnnotation 注释的 bean,因为您要选择的 bean 仅使用 @MyAnnotation 进行了注释。我认为 @Any 在 Instance 注入中不是必需的,因为 Instance 将能够访问 MyInterface 的所有实现,因此您可以选择带有 @MyAnnotation(MyEnum.value) 注释的那个。我从来没有使用带有限定符的实例,它使用枚举。
我认为您不需要使用 Instace。以下注入不起作用吗?
class MyDelegate implements MyInterface{
@Inject
@MyAnnotation(MyEnum.value)
private MyInterface myInterface;
}
如果您需要委托给另一个实现,那么您可能需要 CDI 装饰器。
@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>
这对你有帮助吗?
推荐阅读
- android - 为什么我没有从我的 startActivityForResult 得到结果?
- php - 数组的 print_r 不打印值
- amazon-s3 - AWS s3“放置对象 - 复制”是否异步执行?
- java - Spring拦截授权端点的http请求
- ansible - 防止在 Ansible 中将变量转换为布尔值
- javascript - 从一个 html 文档转到另一个 html 文档时,我需要动态创建元素
- node.js - 如何使用 Node JS 将经过身份验证的用户传递给新视图
- c# - 我怎样才能加快这个“mini-git”课程的速度?
- file - 如何使用 Visual Basic Script/VBscript 获取目录中的文件列表?
- sql-server - 如何在维度表上打乱唯一键值?