Skip to content

NullPointerException in AnnotationAwareOrderComparator#getPriority [SPR-16583] #21125

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: duplicateA duplicate of another issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions