Skip to content

NullPointerException when using @ConditionalOnSingleCandidate with multiple manually registered singletons #48117

@linw-bai

Description

@linw-bai

Description

When registering two singleton beans using SingletonBeanRegistry#registerSingleton and using @ConditionalOnSingleCandidate(TestBean.class) a NullPointerException occurs due to the absence of corresponding bean definitions.

Reproduction Steps

Register singleton bean using SingletonBeanRegistry#registerSingleton:

   beanFactory.registerSingleton("bean1", new TestBean());
   beanFactory.registerSingleton("bean2", new TestBean());

Use @ConditionalOnSingleCandidate(TestBean.class):

@Configuration
@ConditionalOnSingleCandidate(TestBean.class)
public class TestConfiguration {
    @Bean
    public SomeBean someBean() {
        return new SomeBean();
    }
}

The application will throw a NullPointerException during startup.

Environment Information

  • Spring Boot Version: 3.5.7
  • JDK Version: 21

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions