24
24
import java .lang .annotation .Annotation ;
25
25
import java .lang .ref .Reference ;
26
26
import java .lang .ref .WeakReference ;
27
+ import java .lang .reflect .Method ;
27
28
import java .security .AccessController ;
28
29
import java .security .PrivilegedAction ;
29
30
import java .util .ArrayList ;
62
63
import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
63
64
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
64
65
import org .springframework .beans .factory .config .DependencyDescriptor ;
66
+ import org .springframework .core .OrderComparator ;
65
67
import org .springframework .core .annotation .AnnotationUtils ;
66
- import org .springframework .core .annotation .OrderProviderComparator ;
67
- import org .springframework .core .annotation .OrderUtils ;
68
68
import org .springframework .lang .UsesJava8 ;
69
69
import org .springframework .util .Assert ;
70
70
import org .springframework .util .ClassUtils ;
@@ -941,7 +941,7 @@ public Object doResolveDependency(DependencyDescriptor descriptor, String beanNa
941
941
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter ());
942
942
Object result = converter .convertIfNecessary (matchingBeans .values (), type );
943
943
if (this .dependencyComparator != null && result instanceof Object []) {
944
- sortArray ((Object []) result , matchingBeans );
944
+ Arrays . sort ((Object []) result , adaptDependencyComparator ( matchingBeans ) );
945
945
}
946
946
return result ;
947
947
}
@@ -968,7 +968,7 @@ else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
968
968
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter ());
969
969
Object result = converter .convertIfNecessary (matchingBeans .values (), type );
970
970
if (this .dependencyComparator != null && result instanceof List ) {
971
- sortList ((List <?>) result , matchingBeans );
971
+ Collections . sort ((List <?>) result , adaptDependencyComparator ( matchingBeans ) );
972
972
}
973
973
return result ;
974
974
}
@@ -1029,32 +1029,22 @@ else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
1029
1029
}
1030
1030
}
1031
1031
1032
- private void sortArray ( Object [] items , Map <String , Object > matchingBeans ) {
1033
- if (this .dependencyComparator instanceof OrderProviderComparator ) {
1034
- (( OrderProviderComparator ) this .dependencyComparator )
1035
- . sortArray ( items , createFactoryAwareOrderProvider (matchingBeans ));
1032
+ private Comparator < Object > adaptDependencyComparator ( Map <String , Object > matchingBeans ) {
1033
+ if (this .dependencyComparator instanceof OrderComparator ) {
1034
+ return (( OrderComparator ) this .dependencyComparator ). withSourceProvider (
1035
+ createFactoryAwareOrderSourceProvider (matchingBeans ));
1036
1036
}
1037
1037
else {
1038
- Arrays . sort ( items , this .dependencyComparator ) ;
1038
+ return this .dependencyComparator ;
1039
1039
}
1040
1040
}
1041
1041
1042
- private void sortList (List <?> items , Map <String , Object > matchingBeans ) {
1043
- if (this .dependencyComparator instanceof OrderProviderComparator ) {
1044
- ((OrderProviderComparator ) this .dependencyComparator )
1045
- .sortList (items , createFactoryAwareOrderProvider (matchingBeans ));
1046
- }
1047
- else {
1048
- Collections .sort (items , this .dependencyComparator );
1049
- }
1050
- }
1051
-
1052
- private FactoryAwareOrderProvider createFactoryAwareOrderProvider (Map <String , Object > beans ) {
1042
+ private FactoryAwareOrderSourceProvider createFactoryAwareOrderSourceProvider (Map <String , Object > beans ) {
1053
1043
IdentityHashMap <Object , String > instancesToBeanNames = new IdentityHashMap <Object , String >();
1054
1044
for (Map .Entry <String , Object > entry : beans .entrySet ()) {
1055
1045
instancesToBeanNames .put (entry .getValue (), entry .getKey ());
1056
1046
}
1057
- return new FactoryAwareOrderProvider (instancesToBeanNames , this );
1047
+ return new FactoryAwareOrderSourceProvider (instancesToBeanNames );
1058
1048
}
1059
1049
1060
1050
/**
@@ -1223,13 +1213,18 @@ protected boolean isPrimary(String beanName, Object beanInstance) {
1223
1213
/**
1224
1214
* Return the priority assigned for the given bean instance by
1225
1215
* the {@code javax.annotation.Priority} annotation.
1226
- * <p>If the annotation is not present, returns {@code null}.
1227
- * @param beanInstance the bean instance to check (can be null)
1216
+ * <p>The default implementation delegates to the specified
1217
+ * {@link #setDependencyComparator dependency comparator}, checking its
1218
+ * {@link OrderComparator#getPriority method} if it is an extension of
1219
+ * Spring's common {@link OrderComparator} - typically, an
1220
+ * {@link org.springframework.core.annotation.AnnotationAwareOrderComparator}.
1221
+ * If no such comparator is present, this implementation returns {@code null}.
1222
+ * @param beanInstance the bean instance to check (can be {@code null})
1228
1223
* @return the priority assigned to that bean or {@code null} if none is set
1229
1224
*/
1230
1225
protected Integer getPriority (Object beanInstance ) {
1231
- if (beanInstance != null ) {
1232
- return OrderUtils . getPriorityValue ( beanInstance . getClass () );
1226
+ if (this . dependencyComparator instanceof OrderComparator ) {
1227
+ return (( OrderComparator ) this . dependencyComparator ). getPriority ( beanInstance );
1233
1228
}
1234
1229
return null ;
1235
1230
}
@@ -1406,4 +1401,36 @@ public Object createDependencyProvider(DependencyDescriptor descriptor, String b
1406
1401
}
1407
1402
}
1408
1403
1404
+
1405
+ /**
1406
+ * An {@link org.springframework.core.OrderComparator.OrderSourceProvider} implementation
1407
+ * that is aware of the bean metadata of the instances to sort.
1408
+ * <p>Lookup for the method factory of an instance to sort, if any, and let the
1409
+ * comparator retrieve the {@link org.springframework.core.annotation.Order}
1410
+ * value defined on it. This essentially allows for the following construct:
1411
+ */
1412
+ private class FactoryAwareOrderSourceProvider implements OrderComparator .OrderSourceProvider {
1413
+
1414
+ private final Map <Object , String > instancesToBeanNames ;
1415
+
1416
+ public FactoryAwareOrderSourceProvider (Map <Object , String > instancesToBeanNames ) {
1417
+ this .instancesToBeanNames = instancesToBeanNames ;
1418
+ }
1419
+
1420
+ @ Override
1421
+ public Object getOrderSource (Object obj ) {
1422
+ return getFactoryMethod (this .instancesToBeanNames .get (obj ));
1423
+ }
1424
+
1425
+ private Method getFactoryMethod (String beanName ) {
1426
+ if (beanName != null && containsBeanDefinition (beanName )) {
1427
+ BeanDefinition bd = getMergedBeanDefinition (beanName );
1428
+ if (bd instanceof RootBeanDefinition ) {
1429
+ return ((RootBeanDefinition ) bd ).getResolvedFactoryMethod ();
1430
+ }
1431
+ }
1432
+ return null ;
1433
+ }
1434
+ }
1435
+
1409
1436
}
0 commit comments