Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Mar 12, 2018 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Mar 12, 2018

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:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This has been fixed for 5.0.4 already.

@spring-projects-issues
Copy link
Collaborator Author

Manish Kumar commented

Yes, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

2 participants