Custom qualifiers on interface doesn't work on injection

偶尔善良 提交于 2019-12-11 18:21:53

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/36788646/custom-qualifiers-on-interface-doesnt-work-on-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!