We have internal library with common configuration/code for our services. And we want to upgrade used SpringBoot version to address latest "Spring4Shell" vulnerability.
After upgrading SpringBoot 2.5.4 -> 2.5.13 (tried also 2.5.12)
we got BeanCreationException for bean configured inside this library.
(Library declared as a dependency using Gradle + "org.springframework.boot" Gradle plugin.)
There are no other changes - only Spring Boot version upgrade.
Below adding library configuration class as well as error stacktrace.
First error was about public ExecutorService cachedThreadPool() bean (it is very simple as you will see in code below).
And when we replaced this bean with local one (using @Primary annotation), then the same error appears about second bean from this configuration - public Executor getAsyncExecutor()
Configuration class from our library
package com.xxx.xxxpackage.libs.routine.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@EnableAsync
@Configuration
@Profile("routine")
public class RoutineAsyncConfiguration implements AsyncConfigurer {
@Value("${xxx.async.threads.core-size:1}")
private int coreSize;
@Value("${xxx.async.threads.max-size:10}")
private int maxSize;
@Value("${xxx.async.threads.queue-capacity:100}")
private int queueCapacity;
@Value("${xxx.async.threads.format:async-thread-%d}")
private String threadNameFormat;
@Override
@Bean(name = "asyncTaskExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(coreSize);
executor.setMaxPoolSize(maxSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix(threadNameFormat);
return executor;
}
@Bean
public ExecutorService cachedThreadPool() {
return Executors.newCachedThreadPool();
}
}
Usage of the bean (constructor injection, minor details omitted):
package com.xxx.xxxpackage.market.schedule.routine;
...
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.stream.Collectors;
@Slf4j
@Routine
@ConditionalOnProperty(name = "xxx.routine-scheduler.routine.enabled", havingValue = "true")
public class ApixxxRoutine extends BaseRoutine {
...
public ApixxxRoutine(
...
ExecutorService cachedThreadPool,
...
) {
super(cachedThreadPool);
}
...
}
Error report:
rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apixxxRoutine' defined in file [/Users/username/javadev/xxx/xxx_src/xxx-marketxxx/build/classes/java/main/com/xxx/xxxpackage/market/schedule/routine/ApixxxRoutine.class]: Unsatisfied dependency expressed through constructor parameter 7; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cachedThreadPool' defined in class path resource [com/xxx/xxxpackage/libs/routine/configuration/RoutineAsyncConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.concurrent.ExecutorService]: Illegal arguments to factory method 'cachedThreadPool'; args: ; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:448)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:339)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1365)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
at com.xxx.xxxpackage.market.MarketApiApplication.main(MarketApiApplication.java:15)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cachedThreadPool' defined in class path resource [com/xxx/xxxpackage/libs/routine/configuration/RoutineAsyncConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.concurrent.ExecutorService]: Illegal arguments to factory method 'cachedThreadPool'; args: ; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 19 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.concurrent.ExecutorService]: Illegal arguments to factory method 'cachedThreadPool'; args: ; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:172)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 33 common frames omitted
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 34 common frames omitted
Comment From: wilkinsona
Are you using Spring Cloud Sleuth? If so, this is a duplicate of https://github.com/spring-projects/spring-boot/issues/29151. If you are not using Sleuth and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: Rib47
Yes we are using Spring Cloud Sleuth. Thanks for the link! And I see that bug for Sleuth is still opened: https://github.com/spring-cloud/spring-cloud-sleuth/issues/2100
I will take a look on this thread, maybe found some workaround Thanks!
Comment From: wilkinsona
Thanks for letting us know. I'll close this as a duplicate.