Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [sisu-users] binding priority

Hi Igor,

The short answer is yes, if C1 appears before C2 on the classpath then C1 will be picked first.

The long answer is that components are ordered based on their ranking; where components have the same ranking they are ordered by their appearance. If they are in different injectors then it’s which injector was added first, if they are in the same injector then it’s which binding appears first in the binding list. Now binding order is deterministic and depends on the order that the modules were processed, similarly Sisu’s classpath scanner is deterministic and respects the order of jars on the classpath. One caveat: if you use an index which shortcuts the scanning then the order is based on where the component appears in the index (generate the index with 0.3.0 or later as it guarantees the classpath order is maintained in the index).

The other thing to bear in mind is how a component’s ranking is calculated - as Gian says below if you use @Priority this will override the current ranking function. Otherwise the default ranking function, given a primary rank in the injector of N, is to assign “DefaultXYZ” components a ranking of N and non-default components a ranking of N+Integer.MIN_VALUE. This function makes sure default components come before non-default components regardless of the primary rank. The primary rank for any injector defaults to 0, but applications are free to choose a different (non-negative) ranking or even change the function, by binding their own ranking function in the injector.

For example, Plexus assigns an increasing primary rank to new plugins so they can override core components, while still respecting that default components appear before non-default.

Summary: the order of components is deterministic and is influenced by classpath/index order, unless you use @Priority to force a specific ordering.

-- 
Cheers, Stuart

On Monday, 24 August 2015 at 07:53, gian.maria.romanato@xxxxxxxxxxxx wrote:

Hello

You can ensure that C1 will be picked first by annotating it @Priority with a value greater than 0.

There is a @Priority annotation in the JavaEE specification but Sisu also includes its own @Priority for those that don't have the JavaEE classes in the classpath.


Inviato da IBM Notes Traveler


Igor Fedorenko --- [sisu-users] binding priority ---

Da:"Igor Fedorenko" <igor@xxxxxxxxxxxxxx>
A:"" <sisu-users@xxxxxxxxxxx>
Data:Sab, 22/Ago/2015 15:33
Oggetto:[sisu-users] binding priority


I have two components that implement the same component interface @Named @Singleton public class C1 implements Foo {} @Named // not a @Singleton, not sure if this is important public class C2 implements Foo {} My application has single classloader, the two components are coming from different jars, and C1 is before C2 on classpath. Can I assume that "@Inject Foo" will reliably pick C1 based on classpath order, or is there more to it? Thank you in advance. -- Regards, Igor _______________________________________________ sisu-users mailing list sisu-users@xxxxxxxxxxx To change your delivery options, retrieve your password, or unsubscribe from this list, visit https://dev.eclipse.org/mailman/listinfo/sisu-users
_______________________________________________
sisu-users mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit


Back to the top