接口上的自定义限定符不适用于注入 [英] Custom qualifiers on interface doesn't work on injection

查看:205
本文介绍了接口上的自定义限定符不适用于注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:



你好我在java EE上遇到问题,我必须使用像@Inject @Stateless etcetc这样的注释,但我无法得到一个解决方案,在阅读了大量的文档和示例etcetc后,似乎我的代码应该工作,但绝对不是。所以问题是我在这样的界面上有自定义限定符

  @Qualifier 
@Retention(RUNTIME)
@Target({METHOD,FIELD,PARAMETER,TYPE})
public @interface BudgetsDs {
}

我希望将它作为成员字段注入另一个对象中

  @Dependent 
公共类BudgetService {

@BudgetsDs
@Inject
DataSource预算DS;

...一些getter,setter,etcetc ...

}

我已经通过这个



https://docs.oracle.com/javaee/6/tutorial/doc/gjbck.html



<和其他类似的文档。
我试过@EJB,@ Stateless但它不起作用。
我认为我目前在注释上遗漏了一件大事,
所以如果你有很好的教程,或者很好的建议,或者我想听听他们的解释。
我不想要一个完整的答案,但我想至少有一些线索。
欢迎任何人提供帮助。



编辑:
确切错误在线

  @BudgetsDs 
@Inject
DataSource预算DS;

budgetDS throw不满意的依赖关系:没有bean匹配注入点



Edit2 解决方案



1)我是实现了一个新类ResourceProducer
正如John Ament所建议的那样
i使用@Produces和@Resource(name =jdbc / myDataSource),
我需要的每个数据源。

 公共类ResourceProducer {

@Produces
@BudgetsDs
@Resource(name =jdbc / BudgetsDs)
public DataSource budgetsDs;

@Produces
@OtherDs
@Resource(name =jdbc / OtherDs)
public DataSource otherDs;

...

}

2)每个连接的不同自定义限定符

  //在文件
@Qualifier中
@Retention(RUNTIME)
@Target({METHOD,FIELD,PARAMETER,TYPE})
public @interface BudgetsDs {
}

//在另一个文件中
@Qualifier
@Retention(RUNTIME)
@Target({METHOD,FIELD,PARAMETER,TYPE})
public @interface OtherDs {
}

3)这样注入效果很好,我可以注入不同的我想要的DataSource,如下所示:

  @Dependent 
公共类BudgetService {

@BudgetsDs
@Inject
DataSource预算DS;

@OtherDs
@Inject
DataSource otherDs;
...一些getter,setter,etcetc ...

}

还有另一个解决方案,下面也有效。



感谢aribeiro和John Ament。
关于堆栈溢出的第一个问题,社区真的很好地回答了

解决方案

你收到的错误发生了由于容器不知道如何将您创建的限定符与任何bean类型的任何实现相匹配。



作为文档声明:


限定符是您应用于bean的注释


因此,为了克服上述错误,您需要将相应的限定符应用于每个 DataSource 实现:

  @BudgetsDs 
公共类BudgetDataSource扩展DataSource {
(...)
}

@OtherDs
公共类OtherDataSource扩展了DataSource {
(...)
}

然后,容器wil l当你注射你的bean时,你能够做出正确的实现:

  @Dependent 
公共类BudgetService {

@BudgetsDs
@Inject
DataSource预算DS;

(...)
}

@Dependent
公共类OtherService {

@Inject
@OtherDs
DataSource otherDS;

(...)
}

作为一方请注意,我注意到您正在关注Java EE 6文档。我建议您按照最新的 EE EE文档进行操作。


The Problem:

Hello i'm having a problem on java EE , i have to use annotation like @Inject @Stateless etcetc, but i can't get a solution, after reading a lot of documentation and example etcetc it seems that what i 've code should work but definitly not. So the problem is that i have a custom qualifiers on an interface like this

 @Qualifier
 @Retention(RUNTIME)
 @Target({METHOD, FIELD, PARAMETER, TYPE})
 public @interface BudgetsDs {
 }

and i want inject it in another object as a member field like this

@Dependent
public class BudgetService  {

@BudgetsDs
@Inject
DataSource budgetsDS;

... some getter, setter , etcetc...

}

i've pass through this

https://docs.oracle.com/javaee/6/tutorial/doc/gjbck.html

and other kind of documentation similar to this one. I'v tried with @EJB , @Stateless but it doesn't work. I think that i'm currently missing a big thing on the annotation, so if you have good tutorials , or good advice, or explanation i want to hear them. I don't ask for a complete answer but i would like to have at least some clue. Anyone is welcome to help.

Edit: the exact error is on the line

@BudgetsDs
@Inject
DataSource budgetsDS;

budgetDS throw Unsatisfied Dependency: no bean matches the injection point

Edit2: The solution

1) I'v implemented a new class ResourceProducer As suggested by John Ament i use @Produces and @Resource(name="jdbc/myDataSource"), for each DataSource that i need.

public class ResourceProducer {

@Produces
@BudgetsDs
@Resource(name="jdbc/BudgetsDs")
public DataSource budgetsDs;

@Produces
@OtherDs
@Resource(name="jdbc/OtherDs")
public DataSource otherDs;

...  

}

2) The different custom qualifier for every connection

 // In a file 
 @Qualifier
 @Retention(RUNTIME)
 @Target({METHOD, FIELD, PARAMETER, TYPE})
 public @interface BudgetsDs {
 }

 // In a another file
 @Qualifier
 @Retention(RUNTIME)
 @Target({METHOD, FIELD, PARAMETER, TYPE})
 public @interface OtherDs {
 }

3) This way the inject works well and i can inject the different DataSource where i want, like this :

@Dependent
public class BudgetService  {

@BudgetsDs
@Inject
DataSource budgetsDS;

@OtherDs
@Inject
DataSource otherDs;
... some getter, setter , etcetc...

}

There is another solution Below, that works too.

Thanks to aribeiro and John Ament. My first question on stack overflow, and really well answered by the community

解决方案

The error you're receiving occurs due to the fact that the container does not know how to match the qualifier you've created with any implementation of any bean type.

As the documentation states:

A qualifier is an annotation that you apply to a bean

Therefore, in order to overcome the error mentioned, you need to apply the respective qualifier to each DataSource implementation:

@BudgetsDs
public class BudgetDataSource extends DataSource {
    (...)
}

@OtherDs
public class OtherDataSource extends DataSource {
    (...)
}

Then, the container will be able to needle to the right implementation when you're injecting your beans:

@Dependent
public class BudgetService  {

    @BudgetsDs
    @Inject
    DataSource budgetsDS;

    (...)
}

@Dependent
public class OtherService {

    @Inject
    @OtherDs
    DataSource otherDS;

    (...)
}

As a side note, I've noticed that you're following Java EE 6 documentation. I'd suggest you to follow the latest Java EE documentation.

这篇关于接口上的自定义限定符不适用于注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆