Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with RestTemplate Initialization in Spring Cloud Task #952

Open
aiden-sim opened this issue Aug 25, 2024 · 2 comments
Open

Issues with RestTemplate Initialization in Spring Cloud Task #952

aiden-sim opened this issue Aug 25, 2024 · 2 comments

Comments

@aiden-sim
Copy link

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
Copy link
Author

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?

@cppwfs
Copy link
Collaborator

cppwfs commented Aug 26, 2024

Can you provide a simple project that demonstrates the problem?

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants