@@ -1718,18 +1718,73 @@ void mapInjectionWithPriority() {
1718
1718
assertThat (bean .getBeanName ()).isEqualTo ("bd1" );
1719
1719
}
1720
1720
1721
+ /**
1722
+ * {@code determineHighestPriorityCandidate()} should reject duplicate
1723
+ * priorities for the highest priority detected.
1724
+ *
1725
+ * @see #getBeanByTypeWithMultipleNonHighestPriorityCandidates()
1726
+ */
1721
1727
@ Test
1722
- void getBeanByTypeWithMultiplePriority () {
1728
+ void getBeanByTypeWithMultipleHighestPriorityCandidates () {
1723
1729
lbf .setDependencyComparator (AnnotationAwareOrderComparator .INSTANCE );
1724
1730
RootBeanDefinition bd1 = new RootBeanDefinition (HighPriorityTestBean .class );
1725
- RootBeanDefinition bd2 = new RootBeanDefinition (HighPriorityTestBean .class );
1731
+ RootBeanDefinition bd2 = new RootBeanDefinition (LowPriorityTestBean .class );
1732
+ RootBeanDefinition bd3 = new RootBeanDefinition (HighPriorityTestBean .class );
1726
1733
lbf .registerBeanDefinition ("bd1" , bd1 );
1727
1734
lbf .registerBeanDefinition ("bd2" , bd2 );
1735
+ lbf .registerBeanDefinition ("bd3" , bd3 );
1728
1736
1729
1737
assertThatExceptionOfType (NoUniqueBeanDefinitionException .class )
1730
1738
.isThrownBy (() -> lbf .getBean (TestBean .class ))
1731
- .withMessageContaining ("Multiple beans found with the same priority" )
1732
- .withMessageContaining ("5" ); // conflicting priority
1739
+ .withMessageContaining ("Multiple beans found with the same highest priority (5) among candidates: " );
1740
+ }
1741
+
1742
+ /**
1743
+ * {@code determineHighestPriorityCandidate()} should ignore duplicate
1744
+ * priorities for any priority other than the highest, and the order in
1745
+ * which beans is declared should not affect the outcome.
1746
+ *
1747
+ * @see #getBeanByTypeWithMultipleHighestPriorityCandidates()
1748
+ */
1749
+ @ Test // gh-33733
1750
+ void getBeanByTypeWithMultipleNonHighestPriorityCandidates () {
1751
+ getBeanByTypeWithMultipleNonHighestPriorityCandidates (
1752
+ PriorityService1 .class ,
1753
+ PriorityService2A .class ,
1754
+ PriorityService2B .class ,
1755
+ PriorityService3 .class
1756
+ );
1757
+
1758
+ getBeanByTypeWithMultipleNonHighestPriorityCandidates (
1759
+ PriorityService3 .class ,
1760
+ PriorityService2B .class ,
1761
+ PriorityService2A .class ,
1762
+ PriorityService1 .class
1763
+ );
1764
+
1765
+ getBeanByTypeWithMultipleNonHighestPriorityCandidates (
1766
+ PriorityService2A .class ,
1767
+ PriorityService1 .class ,
1768
+ PriorityService2B .class ,
1769
+ PriorityService3 .class
1770
+ );
1771
+
1772
+ getBeanByTypeWithMultipleNonHighestPriorityCandidates (
1773
+ PriorityService2A .class ,
1774
+ PriorityService3 .class ,
1775
+ PriorityService1 .class ,
1776
+ PriorityService2B .class
1777
+ );
1778
+ }
1779
+
1780
+ private void getBeanByTypeWithMultipleNonHighestPriorityCandidates (Class <?>... classes ) {
1781
+ lbf .setDependencyComparator (AnnotationAwareOrderComparator .INSTANCE );
1782
+ for (Class <?> clazz : classes ) {
1783
+ lbf .registerBeanDefinition (clazz .getSimpleName (), new RootBeanDefinition (clazz ));
1784
+ }
1785
+
1786
+ PriorityService bean = lbf .getBean (PriorityService .class );
1787
+ assertThat (bean ).isExactlyInstanceOf (PriorityService1 .class );
1733
1788
}
1734
1789
1735
1790
@ Test
@@ -3519,6 +3574,26 @@ public KnowsIfInstantiated() {
3519
3574
}
3520
3575
3521
3576
3577
+ interface PriorityService {
3578
+ }
3579
+
3580
+ @ Priority (1 )
3581
+ static class PriorityService1 implements PriorityService {
3582
+ }
3583
+
3584
+ @ Priority (2 )
3585
+ static class PriorityService2A implements PriorityService {
3586
+ }
3587
+
3588
+ @ Priority (2 )
3589
+ static class PriorityService2B implements PriorityService {
3590
+ }
3591
+
3592
+ @ Priority (3 )
3593
+ static class PriorityService3 implements PriorityService {
3594
+ }
3595
+
3596
+
3522
3597
@ Priority (5 )
3523
3598
private static class HighPriorityTestBean extends TestBean {
3524
3599
}
0 commit comments