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
This commit is contained in:
parent
7ce709309d
commit
e849bc3e90
|
@ -1205,7 +1205,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
* Return whether the bean definition for the given bean name has been
|
* Return whether the bean definition for the given bean name has been
|
||||||
* marked as a primary bean.
|
* marked as a primary bean.
|
||||||
* @param beanName the name of the bean
|
* @param beanName the name of the bean
|
||||||
* @param beanInstance the corresponding bean instance
|
* @param beanInstance the corresponding bean instance (can be null)
|
||||||
* @return whether the given bean qualifies as primary
|
* @return whether the given bean qualifies as primary
|
||||||
*/
|
*/
|
||||||
protected boolean isPrimary(String beanName, Object beanInstance) {
|
protected boolean isPrimary(String beanName, Object beanInstance) {
|
||||||
|
@ -1221,11 +1221,14 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
* Return the priority assigned for the given bean instance by
|
* Return the priority assigned for the given bean instance by
|
||||||
* the {@code javax.annotation.Priority} annotation.
|
* the {@code javax.annotation.Priority} annotation.
|
||||||
* <p>If the annotation is not present, returns {@code null}.
|
* <p>If the annotation is not present, returns {@code null}.
|
||||||
* @param beanInstance the bean instance to check
|
* @param beanInstance the bean instance to check (can be null)
|
||||||
* @return the priority assigned to that bean or {@code null} if none is set
|
* @return the priority assigned to that bean or {@code null} if none is set
|
||||||
*/
|
*/
|
||||||
protected Integer getPriority(Object beanInstance) {
|
protected Integer getPriority(Object beanInstance) {
|
||||||
return OrderUtils.getPriorityValue(beanInstance.getClass());
|
if (beanInstance != null) {
|
||||||
|
return OrderUtils.getPriorityValue(beanInstance.getClass());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1403,6 +1403,17 @@ public class DefaultListableBeanFactoryTests {
|
||||||
lbf.getBean(TestBean.class);
|
lbf.getBean(TestBean.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBeanByTypeWithPriorityAndNullInstance() throws Exception {
|
||||||
|
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||||
|
RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
|
||||||
|
RootBeanDefinition bd2 = new RootBeanDefinition(NullTestBeanFactoryBean.class);
|
||||||
|
lbf.registerBeanDefinition("bd1", bd1);
|
||||||
|
lbf.registerBeanDefinition("bd2", bd2);
|
||||||
|
TestBean bean = lbf.getBean(TestBean.class);
|
||||||
|
assertThat(bean.getBeanName(), equalTo("bd1"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() throws Exception {
|
public void testGetBeanByTypePrimaryHasPrecedenceOverPriority() throws Exception {
|
||||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||||
|
@ -2893,5 +2904,23 @@ public class DefaultListableBeanFactoryTests {
|
||||||
@Priority(500)
|
@Priority(500)
|
||||||
private static class LowPriorityTestBean extends TestBean {}
|
private static class LowPriorityTestBean extends TestBean {}
|
||||||
|
|
||||||
|
private static class NullTestBeanFactoryBean<T> implements FactoryBean<TestBean> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TestBean getObject() throws Exception {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getObjectType() {
|
||||||
|
return TestBean.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isSingleton() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue