You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for @javax.annotation.Priority as alternative to @Primary [SPR-10548] #15179
If multiple beans are candidates to satisfy an @Autowired/@Inject dependency, Spring looks for the @Primary annotation on one of the beans. If it finds it, it uses that bean; otherwise, it throws an exception.
javax.annotation version 1.2 (Java EE 7 and in theory Java 8) adds a new annotation to the common annotations, @Priority.
@Priority includes a single integer attribute that defines what the priority is. My suggestion is that if multiple beans are candidates to satisfy an @Autowired/@Inject dependency and @Primary is not present, Spring should look for @Priority instead. If one or more of the beans have @Priority, the one with the highest priority should be used. If two or more beans are tied for highest priority, it should behave the same as if two or more beans were @Primary.
@javax.annotation.Priority looks a lot like @org.springframework.core.annotation.Order at first glance. Do have a specific use-case where this kind of support would be useful?
I did a similar extension in a spring mvc application.
First i used @ordered but came finally up with a @precedence annotation.
I felt better with Precedence as i have implemented some predefined constants for it like FRAMEWORK, APPLICATION and MOCK.
I used it to implement two scenarios:
1: testing: having MOCK precedence easily allowed us unit and integration testing with adding the desired class in the scanned path to superseed beans without additional configuration. Mailsender for example.
2: default behaviors which can be changed for client needs by simply adding the desired (custom) package for the client, if the default behavior was not enough. One example here would be our standard transition configuration for an entity which basically maps CRUD. Some need just that, other transition flows need to have a more sofisticated "workflow" with additional stati like ARCHIVED, DRAFT, etc.
Having this precedence implementation in the BeanFactory's determinePrimaryCandidate method allowed us simple configuration by adding classes.
The initial idea comes from JBoss Seam which had this exact behaviour.
I should be able to provide you with my implemetation of that feature for spring 3.2 later ones i have access to my machine.
Nick, is there any reason why you mentioned @Order? AFAIK, order is not used at all. Mixing @Priority with @Order might not be such a good idea as @Order can already be set on existing beans for other purposes.
I recommend therefore to only implement a support for @Priority as you mentioned in your original description and leave @Order for other use cases.
No, I never suggested that it's wise to mix any two or three of @Priority, @Primary, or @Order. It's not. My suggestion is: Where Spring currently supports @Primary, it should support @Priority as an alternative to @Primary; and where Spring currently supports @Order, it should support @Priority as an alternative to @Order.
OK. Could you please raise a separate issue for the @Priority alternative for @Order? This is really a completely different request and it would be better to track those separately.
This is now available in master and in the next 4.1.0.BUILD-SNAPSHOT.
When multiple candidates are available for a given bean, the bean annotated with @Primary is used. If none exists, the one with the higher value for the @Priority annotation is used. If two beans have the same priority a NoUniqueBeanDefinitionException is thrown, just as if two beans are annotated with @Primary.
Do we actually have a follow-up issue here for supporting @Priority as an alternative to @Order? It'd be an interesting variation, since @Primary and @Order are basically independent characteristics at this point (for injection purposes), whereas @Priority would then be indicative for both purposes...
I am not sure I understand what you mean there. There is no "common" behaviour for the standard annotation
The effect of using the Priority annotation in any particular instance is defined by other specifications that define the use of a specific class.
The current code makes sure that the lowest value has the highest priority (to align with what @Order provides). Do you mean you'd like to reverse that? On what ground? Thanks!
I just tried v4.1 today and removed my originally superseding version of the bean factory.
For me it was strange to see that an @Order ed list results in the lowest number being the first element and an auto wired single field resulted in the wiring of the last element of that list as the @Primary.
Basically my test included defining an interface with two implementations. One without priority and one with a "high" priority according to the @Order behavior.
The expected result was that the ordered list first element is the same as the resulting primary.
This was not true using the snapshot of today. The current determineHighestPriorityCandidate implementation uses the highest integer value found, while the AnnotationAwareOrderComparator does the inverse.
This would mean that to make sure that the ordered list first element and the primary object matches, the class would need to define both annotations @Order and @Priority with the "exact" inverted integer to achieve the expected behavior.
classOrderingTest {
@AutowiredprivateList<SomeInterface> someInterfaces;
@AutowiredprivateSomeInterfaceprimaryInterface;
publicvoidtest() {
Assert.assertEquals(2, someInterfaces);
Assert.assertEquals(HighestInterface.class, someInterfaces.get(0).getClass());
Assert.assertEquals(DefaultInterface.class, someInterfaces.get(1).getClass());
// these failAssert.assertEquals(HighestInterface.class, primaryInterface.getClass());
// first element of ordered list should be the primaryAssert.assertEquals(someInterfaces.get(0).getClass(), primaryInterface.getClass());
}
EDIT: Here's the "offending" code: DefaultListableBeanFactory.java Line 1161:
Martin, thank you so much for testing this. You are right, I got confused (again!) with the semantic of order (highest precedence means low value). My brain does not want to accommodate with it ;)
It also turns out we may be missing one test here that validates the Primary/Priority and collection ordering. I'll work on that and let you know when I am done.
Activity
spring-projects-issues commentedon Jul 2, 2013
Phil Webb commented
@javax.annotation.Prioritylooks a lot like@org.springframework.core.annotation.Orderat first glance. Do have a specific use-case where this kind of support would be useful?spring-projects-issues commentedon Dec 26, 2013
Martin Frey commented
I did a similar extension in a spring mvc application.
First i used
@orderedbut came finally up with a@precedenceannotation.I felt better with Precedence as i have implemented some predefined constants for it like FRAMEWORK, APPLICATION and MOCK.
I used it to implement two scenarios:
1: testing: having MOCK precedence easily allowed us unit and integration testing with adding the desired class in the scanned path to superseed beans without additional configuration. Mailsender for example.
2: default behaviors which can be changed for client needs by simply adding the desired (custom) package for the client, if the default behavior was not enough. One example here would be our standard transition configuration for an entity which basically maps CRUD. Some need just that, other transition flows need to have a more sofisticated "workflow" with additional stati like ARCHIVED, DRAFT, etc.
Having this precedence implementation in the BeanFactory's determinePrimaryCandidate method allowed us simple configuration by adding classes.
The initial idea comes from JBoss Seam which had this exact behaviour.
I should be able to provide you with my implemetation of that feature for spring 3.2 later ones i have access to my machine.
spring-projects-issues commentedon Dec 30, 2013
Martin Frey commented
Here's the code I'm currently using in a 3.2 Spring MVC project:
spring-projects-issues commentedon Jan 13, 2014
Stéphane Nicoll commented
Nick, is there any reason why you mentioned
@Order? AFAIK, order is not used at all. Mixing@Prioritywith@Ordermight not be such a good idea as@Ordercan already be set on existing beans for other purposes.I recommend therefore to only implement a support for
@Priorityas you mentioned in your original description and leave@Orderfor other use cases.spring-projects-issues commentedon Jan 13, 2014
Nick Williams commented
No, I never suggested that it's wise to mix any two or three of
@Priority,@Primary, or@Order. It's not. My suggestion is: Where Spring currently supports@Primary, it should support@Priorityas an alternative to@Primary; and where Spring currently supports@Order, it should support@Priorityas an alternative to@Order.spring-projects-issues commentedon Jan 13, 2014
Stéphane Nicoll commented
OK. Could you please raise a separate issue for the
@Priorityalternative for@Order? This is really a completely different request and it would be better to track those separately.Thanks!
spring-projects-issues commentedon Jan 17, 2014
Stéphane Nicoll commented
See PR #442
spring-projects-issues commentedon Mar 31, 2014
Stéphane Nicoll commented
This is now available in master and in the next
4.1.0.BUILD-SNAPSHOT.When multiple candidates are available for a given bean, the bean annotated with
@Primaryis used. If none exists, the one with the higher value for the@Priorityannotation is used. If two beans have the same priority aNoUniqueBeanDefinitionExceptionis thrown, just as if two beans are annotated with@Primary.spring-projects-issues commentedon Mar 31, 2014
Juergen Hoeller commented
Do we actually have a follow-up issue here for supporting
@Priorityas an alternative to@Order? It'd be an interesting variation, since@Primaryand@Orderare basically independent characteristics at this point (for injection purposes), whereas@Prioritywould then be indicative for both purposes...Juergen
spring-projects-issues commentedon Mar 31, 2014
Stéphane Nicoll commented
Right, #16262 has been created for that. Thanks for the reminder Jürgen.
spring-projects-issues commentedon Jul 7, 2014
Martin Frey commented
I'd like to reopen this issue as I think this needs alignment with the current
@Orderimplementation:Is it intended that this implementation works inverse to the
@Orderimplementation?@Priority-> Highest Priority = MAX_INTEGER@Order-> Highest Priority = MIN_INTEGERI would say this should be aligned.
Else great job.
spring-projects-issues commentedon Jul 7, 2014
Stéphane Nicoll commented
Hi Martin,
I am not sure I understand what you mean there. There is no "common" behaviour for the standard annotation
The current code makes sure that the lowest value has the highest priority (to align with what
@Orderprovides). Do you mean you'd like to reverse that? On what ground? Thanks!spring-projects-issues commentedon Jul 7, 2014
Phil Webb commented
Martin Frey I have a feeling that the semantics of the
valuefield intentionally differ from@Order. Looking at the javadoc for@Priorityit appears that negative numbers at least should not be used (https://javaee-spec.java.net/nonav/javadocs/javax/annotation/Priority.html)spring-projects-issues commentedon Jul 7, 2014
Martin Frey commented
I just tried v4.1 today and removed my originally superseding version of the bean factory.
For me it was strange to see that an
@Ordered list results in the lowest number being the first element and an auto wired single field resulted in the wiring of the last element of that list as the@Primary.Basically my test included defining an interface with two implementations. One without priority and one with a "high" priority according to the
@Orderbehavior.The expected result was that the ordered list first element is the same as the resulting primary.
This was not true using the snapshot of today. The current determineHighestPriorityCandidate implementation uses the highest integer value found, while the AnnotationAwareOrderComparator does the inverse.
This would mean that to make sure that the ordered list first element and the primary object matches, the class would need to define both annotations
@Orderand@Prioritywith the "exact" inverted integer to achieve the expected behavior.EDIT: Here's the "offending" code: DefaultListableBeanFactory.java Line 1161:
spring-projects-issues commentedon Jul 7, 2014
Stéphane Nicoll commented
Martin, thank you so much for testing this. You are right, I got confused (again!) with the semantic of order (highest precedence means low value). My brain does not want to accommodate with it ;)
It also turns out we may be missing one test here that validates the Primary/Priority and collection ordering. I'll work on that and let you know when I am done.
Thanks again!
spring-projects-issues commentedon Jul 8, 2014
Stéphane Nicoll commented
The semantic inconsistency has been fixed. A new
4.1.0.BUILD-SNAPSHOTwill be available soon. Please give it a try if you can.Thanks!
spring-projects-issues commentedon Jul 8, 2014
Martin Frey commented
Nice work. It's working fine now :) Thanks a lot.
(By the way: Looks like my supported applications run in 4.1 without a problem until now.)