spring-cloud-task icon indicating copy to clipboard operation
spring-cloud-task copied to clipboard

Issues with RestTemplate Initialization in Spring Cloud Task

Open aiden-sim opened this issue 1 year ago • 2 comments

I'm currently facing a problem where the RestTemplate isn't being initialized properly when performing a Spring Cloud Task. I’ve tried searching for possible causes but haven't been able to pinpoint the root of the issue.

stacktrace

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in demo.CoreApplication: Failed to instantiate [org.springframework.web.client.RestTemplate]: Illegal factory instance for factory method 'restTemplate'; instance: org.springframework.cloud.task.configuration.observation.ObservationCommandLineRunner
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:648)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:485)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1337)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1167)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1443)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:904)
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:782)
	... 74 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Illegal factory instance for factory method 'restTemplate'; instance: org.springframework.cloud.task.configuration.observation.ObservationCommandLineRunner
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:159)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644)
	... 88 common frames omitted
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.checkReceiver(DirectMethodHandleAccessor.java:197)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:99)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140)
	... 89 common frames omitted

I'm using the following versions in my setup:

Java21
Spring Boot 3.2.7
spring-cloud-starter-task:3.1.1

aiden-sim avatar Aug 25 '24 10:08 aiden-sim

It seems I have identified the cause. The current configuration of the application is as follows:


public class CoreApplication implements CommandLineRunner {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}


@SpringBootApplication
@EnableTask
public class DemoTaskApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(DemoTaskApplication.class)
                .sources(CoreApplication.class)
                .run(args);
    }
}

In CoreApplication, there is an implementation of CommandLineRunner that seems to be causing a conflict. When I remove the CommandLineRunner from CoreApplication, it functions correctly.

Could the conflict have arisen because the ObservationCommandLineRunner is importing CoreApplication, leading to the clash?

aiden-sim avatar Aug 25 '24 10:08 aiden-sim

Can you provide a simple project that demonstrates the problem?

Thank you!

cppwfs avatar Aug 26 '24 13:08 cppwfs