Need for injection @Qualifier?

You are (kind of) right, your example works without qualifiers. But its rather that your example is a bit misleading than that you don't need qualifiers.

You are (kind of) right, your example works without qualifiers. But its rather that your example is a bit misleading than that you don't need qualifiers. In general, you will need qualifiers whenever you have have more than one managed bean of a certain type eligible for injection.

This is not the case in your example, but would easily be if you wrote your code like this: public class Breakfast { @Inject Eatable somethingToEat; } (This gives you the flexibility to change your implementation later, for the same reason as you normally write List list = new ArrayList()) You will not need qualifiers if you have just one managed bean of a certain type eligible for injection. More serious examples where you want to use qualifiers would look like this: Imagine you want have a class Locale in your system. Using different qualifiers (together with different producer methods) would allow you to write code like this: ... @Inject @DefaultLocale Locale theDefaultLocale; ... @Inject @StandardLocale Locale theStandardLocale; ... @Inject Instance allLocales; ... To summarize: You need qualifiers if and only if you have more than one bean of a type.

This makes qualifiers redundant for the overwhelming majority of you beans - but you will certainly need them. All this and much more is best read here.

Thanks very much for the answer! And you're right, probably official Weld documentation is the place where I needed to start looking for the answer. In our quite large project we use just a small subset of what CDI spec offers.

That's why after coding for half a year with CDI, I still have some major gaps in understanding some core concepts. But I guess that I could use somehow the notion of qualifiers. – jFrenetic Sep 9 at 21:40.

Yes, always. In fact, @Default is a built in qualifier that informs CDI to inject the default bean implementation. If you define a bean with no qualifier, the bean automatically has the qualifier @Default.

So, in your example, the code : public class Breakfast { @Inject Apple somethingToEat; } could have been written public class Breakfast { @Inject @Default Apple somethingToEat; } As you have already been answered, when you need more than one bean that implements the same bean type, you can qualify an injection point to specify exactly which bean must be injected. Qualifiers also have other benefits. For example, in other frameworks (Seam and Spring) dependencies work mostly by naming beans and binding them to their injection points by their names, CDI instead eliminate reliance on string-base names using @Qualifiers.

Using qualifiers you have eclipse tools specific for CDI like Jboss Tools.

Thanks a lot, it was really helpful! – jFrenetic Sep 10 at 12:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions