[cdi-dev] [JBoss JIRA] (CDI-535) cdi instance injection ordering

Martin Kouba (JIRA) issues at jboss.org
Fri Sep 30 02:31:00 EDT 2016

    [ https://issues.jboss.org/browse/CDI-535?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13300709#comment-13300709 ] 

Martin Kouba commented on CDI-535:

One problem with {{javax.annotation.Priority}} is that its {{@Target}} is currently only TYPE (unfortunately Commons Annotations 1.3 MR only adds PARAMETER). So we will not be able to add priority to producer methods and fields.

> cdi instance injection ordering
> -------------------------------
>                 Key: CDI-535
>                 URL: https://issues.jboss.org/browse/CDI-535
>             Project: CDI Specification Issues
>          Issue Type: Feature Request
>          Components: Beans
>    Affects Versions: 2.0 (discussion)
>            Reporter: Ochieng Marembo
>            Priority: Optional
> We should allow ordering of bean instance injection using the ```Instance<MyBeanInterface>``` when an instance injection is used.
> h3. Use case:
> Developer always define a kind of chain of processor beans, which may need to run in specific order.
> h3. Current scenario:
> Using the Instance injection mechanism, a developer can inject multiple beans implementing the same interface and iterate of them. In order to ensure ordering, the developer could do one of the following:
> #1
> {code}
> private Iterable<MyBeanInterface> myBeans;
> @Inject
> void injectBeans(@Any Instance<MyBeanInterface> myBeans) {
>   //the create order does some kind of ordering on the beans.
>    this.myBeans = createOrder(myBeans);
> }
> {code}
> #2
> This second option may be expensive if we have to order the beans everytime we execute the logic, and if this bean is applicationscoped, it wont make sense to do the ordering in the method call.
> {code}
> @Any
> @Inject
> private Instance<MyBeanInterface> myBeans;
> public void doSomething() {
>   Iterable<MyBeanInterface> orderedbeans = createOrder(myBeans.select(someQualifier));
> }
> {code}
> h3. Our Proposal
> We already have {code}javax.annotation.Priority{code} or any cdi specific annotation which we can add to {code}MyBeanInterfaceImpl{code} so that on injection of an {code}Instance<MyBeanInterface>{code}, all possible injection values are sorted based on the {code}Priority.value(){code} and if no annotation is defined, defaults to {code}Priority.value = Integer.MAX_VALUE{code}
> {code}
> public interface MyBeanInterface {}
> @MyQualifier
> @Priority(0)
> public class MyFirstBean implements MyBeanInterface{
> }
> @MyQualifier
> @Priority(2)
> public class MySecondBean implements MyBeanInterface{
> }
> @ApplicationScoped
> public class MyBeanProcessor {
>   //We expect that this injected instances shall be in order based on the @Priority annotation
>   @Any
>   @Inject
>   private Instance<MyBeanInterface> myBeans;
> }
> {code}

This message was sent by Atlassian JIRA

More information about the cdi-dev mailing list