Skip to content

Commit e849bc3

Browse files
committed
Fix potential NPE
This commit fixes a potential NPE when determining the priority of a bean instance in case multiple candidates exist and no bean was marked as @primary Issue: SPR-12024
1 parent 7ce7093 commit e849bc3

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ else if (candidatePriority < highestPriority) {
12051205
* Return whether the bean definition for the given bean name has been
12061206
* marked as a primary bean.
12071207
* @param beanName the name of the bean
1208-
* @param beanInstance the corresponding bean instance
1208+
* @param beanInstance the corresponding bean instance (can be null)
12091209
* @return whether the given bean qualifies as primary
12101210
*/
12111211
protected boolean isPrimary(String beanName, Object beanInstance) {
@@ -1221,11 +1221,14 @@ protected boolean isPrimary(String beanName, Object beanInstance) {
12211221
* Return the priority assigned for the given bean instance by
12221222
* the {@code javax.annotation.Priority} annotation.
12231223
* <p>If the annotation is not present, returns {@code null}.
1224-
* @param beanInstance the bean instance to check
1224+
* @param beanInstance the bean instance to check (can be null)
12251225
* @return the priority assigned to that bean or {@code null} if none is set
12261226
*/
12271227
protected Integer getPriority(Object beanInstance) {
1228-
return OrderUtils.getPriorityValue(beanInstance.getClass());
1228+
if (beanInstance != null) {
1229+
return OrderUtils.getPriorityValue(beanInstance.getClass());
1230+
}
1231+
return null;
12291232
}
12301233

12311234
/**

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,17 @@ public void testGetBeanByTypeWithMultiplePriority() throws Exception {
14031403
lbf.getBean(TestBean.class);
14041404
}
14051405

1406+
@Test
1407+
public void testGetBeanByTypeWithPriorityAndNullInstance() throws Exception {
1408+
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
1409+
RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
1410+
RootBeanDefinition bd2 = new RootBeanDefinition(NullTestBeanFactoryBean.class);
1411+
lbf.registerBeanDefinition("bd1", bd1);
1412+
lbf.registerBeanDefinition("bd2", bd2);
1413+
TestBean bean = lbf.getBean(TestBean.class);
1414+
assertThat(bean.getBeanName(), equalTo("bd1"));
1415+
}
1416+
14061417
@Test
14071418
public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() throws Exception {
14081419
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
@@ -2893,5 +2904,23 @@ private static class HighPriorityTestBean extends TestBean {}
28932904
@Priority(500)
28942905
private static class LowPriorityTestBean extends TestBean {}
28952906

2907+
private static class NullTestBeanFactoryBean<T> implements FactoryBean<TestBean> {
2908+
2909+
@Override
2910+
public TestBean getObject() throws Exception {
2911+
return null;
2912+
}
2913+
2914+
@Override
2915+
public Class<?> getObjectType() {
2916+
return TestBean.class;
2917+
}
2918+
2919+
@Override
2920+
public boolean isSingleton() {
2921+
return true;
2922+
}
2923+
}
2924+
28962925

28972926
}

0 commit comments

Comments
 (0)