Skip to content

BeanNotOfRequiredTypeException if @Bean factory method returns null #34543

Closed
@felipe-issa-zebra

Description

@felipe-issa-zebra

I've upgraded our app from spring 3.3.5 to 3.4.3 and on the newer version the app is throwing the following error on initialization:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.task.TaskExecutorMetricsAutoConfiguration': Injection of autowired dependencies failed
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:515) ~[spring-beans-6.2.3.jar:6.2.3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1445) ~[spring-beans-6.2.3.jar:6.2.3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) ~[spring-beans-6.2.3.jar:6.2.3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523) ~[spring-beans-6.2.3.jar:6.2.3]
	... many more
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'splitFilesTaskExecutor' is expected to be of type 'org.springframework.core.task.TaskExecutor' but was actually of type 'org.springframework.beans.factory.support.NullBean'
	at org.springframework.beans.factory.support.AbstractBeanFactory.adaptBeanInstance(AbstractBeanFactory.java:424) ~[spring-beans-6.2.3.jar:6.2.3]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:405) ~[spring-beans-6.2.3.jar:6.2.3]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207) ~[spring-beans-6.2.3.jar:6.2.3]
	... many more

This spring bean is created the following way:

@Configuration
public class ProjectLauncherConfiguration {

	@Bean()
	public TaskExecutor splitFilesTaskExecutor() {
		if ( ** some logic ** ) {
			ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
			executor.setMaxPoolSize(splitMaxPoolSize);
			executor.setCorePoolSize(splitPoolSize);
			executor.setQueueCapacity(Integer.MAX_VALUE);
			return executor;
		}
		return null;
	}
}

If the bean is created, it works just fine. But when the scenario doesn't create the bean, the exception is thrown and the app is does not initialize.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions