Java Magazine, Nov/Dec 2017
ORACLE COM JAVAMAGAZINE NOVEMBER DECEMBER 2017 28 java ee 8 Note perhaps somewhat counterintuitively that the @ Alternative annotation is put on the bean hosting the producer method not on the producer method itself A small challenge here is to obtain the bean with type HttpAuthenticationMechanism that would have been chosen by the CDI runtime had the producer not been there For a decorator this is easy because CDI makes that exact bean injectable via the @ Decorated qualifier Here this will have to be done manually One way is to get all the beans of type HttpAuthenticationMechanism from the bean manager this will include both alternatives and nonalternatives filter the producer bean ApplicationInit from that set and then let the bean manager resolve the set to the one that would be chosen for injection After that a reference is created for that chosen bean The following shows this process in code HttpAuthenticationMechanism mechanism createRef beanManager resolve beanManager getBeans HttpAuthenticationMechanism class stream filter e e getBeanClass equals ApplicationInit class collect toSet beanManager Note that the code filters on the getBeanClass outcome The BeanClass perhaps somewhat confusingly does not necessarily represent the class or classes of the bean itself but rather where the bean is created For a producer this is the bean that contains the producer method so that is a very good handle for filtering out the current producer bean The createRef method is defined as follows HttpAuthenticationMechanism createRef Bean bean BeanManager beanManager
You must have JavaScript enabled to view digital editions.