简体   繁体   中英

@Inject Instance with custom quailfier annotation on class type

Lets say we have

class TestClass1 {  

@Inject @Any @BBB(PARAM1)  
Instance< Product > instaces;

@Produces @BBB(PARAM1)  
Product first(){...}

@Produces @BBB(PARAM1)  
Product second(){...}    
}

and

class TestClass2 {  

@Inject @Any @BBB(PARAM2)  
Instance< Product > instaces;

@Produces @BBB(PARAM2)  
Product first(){...}

@Produces @BBB(PARAM2)  
Product second(){...}    
}

When i put @BBBs on class and remove from methods instances are empty. Is how it works and I have to replicate @BBB on every @Produces methods OR something I have missed?

@BBB is annotated with @Qualifier and RUNTIME retention

Not sure I fully understand your question, but I hope this will clarify it. I think you are mixing several things together.

1) Producers

Those are methods/fields which are effectively considered beans ! This means that on top of producer you have to define what kind of bean will it create. That boils down to qualifiers , scope , stereotypes ,... Whatever you need/want there. This also allows you to have multiple producers which will only differ in, say, qualifier with which they create the bean. So yes, you need to repeat it on each producer, for it may make a great difference.

2) Your classes

Your are themselves beans (dependent scope in this case) and you can specify all the CDI stuff on them - scope, qualifiers, etc. This will not propagate to the producers inside the beans. If you think about it, that would mean that should your class be application scoped, anything it produces would also be application scoped, which is not the case.

Your source of confusion might also be interceptor/decorator (or rather their binding) which can be placed either on bean method (enabling interception for given method) or on top of whole class (enabling interception for all methods).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM