From b78b2e9a03f5a6b7924dda0f8d709a5a9b23e24c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 8 Jul 2014 10:26:03 +0200 Subject: [PATCH] Fix priority semantic Commit 5fe8f52 introduced a support for @Priority as an alternative to @Primary but it broke the semantic of the priority value. This commit fixes this inconsistency. As for @Order, the lowest value means the highest priority so if several beans are candidates for injection, the one having the lowest value will be used. Issue: SPR-10548 --- .../beans/factory/support/DefaultListableBeanFactory.java | 6 ++++-- .../beans/factory/DefaultListableBeanFactoryTests.java | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index ef1a6d267e..eedad5dda1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -1136,7 +1136,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto } /** - * Determine the candidate with the highest priority in the given set of beans. + * Determine the candidate with the highest priority in the given set of beans. As + * defined by the {@link org.springframework.core.Ordered} interface, the lowest + * value has the highest priority. * @param candidateBeans a Map of candidate names and candidate instances * that match the required type * @param requiredType the target dependency type to match against @@ -1158,7 +1160,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto "Multiple beans found with the same priority ('" + highestPriority + "') " + "among candidates: " + candidateBeans.keySet()); } - else if (candidatePriority > highestPriority) { + else if (candidatePriority < highestPriority) { highestPriorityBeanName = candidateBeanName; highestPriority = candidatePriority; } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index 618ad67c2f..784f0faa23 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -1399,7 +1399,7 @@ public class DefaultListableBeanFactoryTests { lbf.registerBeanDefinition("bd2", bd2); thrown.expect(NoUniqueBeanDefinitionException.class); thrown.expectMessage(containsString("Multiple beans found with the same priority")); - thrown.expectMessage(containsString("500")); // conflicting priority + thrown.expectMessage(containsString("5")); // conflicting priority lbf.getBean(TestBean.class); } @@ -1613,7 +1613,7 @@ public class DefaultListableBeanFactoryTests { // expected assertNotNull("Exception should have cause", ex.getCause()); assertEquals("Wrong cause type", NoUniqueBeanDefinitionException.class, ex.getCause().getClass()); - assertTrue(ex.getMessage().contains("500")); // conflicting priority + assertTrue(ex.getMessage().contains("5")); // conflicting priority } } @@ -2887,10 +2887,10 @@ public class DefaultListableBeanFactoryTests { } - @Priority(500) + @Priority(5) private static class HighPriorityTestBean extends TestBean {} - @Priority(5) + @Priority(500) private static class LowPriorityTestBean extends TestBean {}