0

Say I have custom type annotations @Classic and @Custom and I have annotated classes BarClassic and BarCustom which both implement interface Bar.

Then say I have another class BarUser where Bar is injected in constructor.

public class BarUser {

    @Inject
    private BarUser (Bar bar) {
        ...
    }

And a class Foo where I would like to inject BarUser, but would like to specify to use say BarClassic within the injected BarUser.

Is there a way to this with annotations. So what I would like is:

public class Foo {

    @Inject
    @Classic
    private BarUser barUser;

    ....

And with this I want to specify that the Bar injected within the BarUser should be the @Classic one. And maybe in another class do:

public class DifferentFoo {

    @Inject
    @Custom
    private BarUser barUser;

    ....

Is the only way to this to have two different BarUser classes as well or is there some magic I could use?

Sunny
  • 605
  • 10
  • 35
  • https://stackoverflow.com/questions/5415261/what-is-javax-inject-named-annotation-supposed-to-be-used-for – C-Otto Jun 14 '17 at 11:38
  • That would work if I had two different BarUser classes, I am aware. I would like to find out if there is a way to do this without the additional class. – Sunny Jun 14 '17 at 11:42

0 Answers0