Closed
Description
Manish Kumar opened SPR-16583 and commented
This issue is similar to #20378 but at a different code location.
We have registered 2 instances of same bean, one is nullable and other is not-nullable. Now we use/inject these beans in some other components as follows:
@Autowired(required = false)
private MyConfig nullableBean;
@Autowired
private MyConfig nonNullableBean;
We get NPE in Spring 5.0.3 while 4.3.10 used to work fine.
Caused by: java.lang.NullPointerException: null
at org.springframework.core.annotation.AnnotationAwareOrderComparator.getPriority(AnnotationAwareOrderComparator.java:109)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getPriority(DefaultListableBeanFactory.java:1471)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.determineHighestPriorityCandidate(DefaultListableBeanFactory.java:1417)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.determineAutowireCandidate(DefaultListableBeanFactory.java:1348)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1113)
What we found that older version(4.3.10) of Spring had null check and then in 5.0.3 we seems to have removed it. See below code from AnnotationAwareOrderComparator class from 5.0.3.
public Integer getPriority(Object obj) {
if (obj instanceof Class) {
return OrderUtils.getPriority((Class<?>) obj);
}
Integer priority = OrderUtils.getPriority(obj.getClass());
if (priority == null && obj instanceof DecoratingProxy) {
priority = OrderUtils.getOrder(((DecoratingProxy) obj).getDecoratedClass());
}
return priority;
}
Here if obj is NULL, NPE will happen. Spring-core 4.3.10 had NULL check for same. Below is code snippet from Spring-core 4.3.10 which has proper NULL check.
public Integer getPriority(Object obj) {
Integer priority = null;
if (obj instanceof Class) {
priority = OrderUtils.getPriority((Class<?>) obj);
}
else if (obj != null) {
priority = OrderUtils.getPriority(obj.getClass());
if (priority == null && obj instanceof DecoratingProxy) {
priority = OrderUtils.getOrder(((DecoratingProxy) obj).getDecoratedClass());
}
}
return priority;
}
Affects: 5.0.3
Issue Links:
- NPE in AnnotationAwareOrderComparator.getPriority [SPR-16508] #21051 NPE in AnnotationAwareOrderComparator.getPriority ("duplicates")
- AnnotationAwareOrderComparator doesn't handle null values anymore [SPR-15823] #20378 AnnotationAwareOrderComparator doesn't handle null values anymore