Home > Net >  Failed to instantiate: No argument resolvers: afterPropertiesSet was not called
Failed to instantiate: No argument resolvers: afterPropertiesSet was not called

Time:07-27

I Created a Simple Spring Boot Project with Spring Initializer. I Call an External Service with This Component:

public interface HotelService{

@PostExchange("http://localhost:9095/api/hotel/v1/")
HotelResponse getHotel(@RequestBody getHotelRequest request);

}

I injected this Component to a Rest Controller class and Used it there. Also in ClientConfig I created the Bean like this:

@Bean
HotelService hotelService(WebClient webClient){
    HttpServiceProxyFactory factory = WebClientAdapter.createHttpServiceProxyFactory(webClient);
    return factory.createClient(HotelService.class);
}

WebClient Bean:

@Bean
WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
    ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
        new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
    oauth2Client.setDefaultClientRegistrationId("hotel");
    return WebClient.builder()
        .apply(oauth2Client.oauth2Configuration())
        .build();
}

but When I Run the Project it throws an Exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelService' defined in class path resource [com/example/demo/ClientConfig.class]: Failed to instantiate [com.example.demo.HotelService]: No argument resolvers: afterPropertiesSet was not called
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:633) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:621) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1323) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:566) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:526) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1374) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1294) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:861) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:765) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
... 19 common frames omitted


Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.demo.HotelService]: No argument resolvers: afterPropertiesSet was not called
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:161) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:629) ~[spring-beans-6.0.0-M5.jar:6.0.0-M5]
... 33 common frames omitted

Any Idea? How can I fix it?

Edit:

spring boot version 3.0.0-M4 jdk version 17

CodePudding user response:

Create a web client

@Bean
WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
    ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
            new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
    oauth2Client.setDefaultClientRegistrationId("hotel");
    return WebClient.builder()
            .baseUrl("http://localhost:9095/api/hotel/v1/")
            .apply(oauth2Client.oauth2Configuration())
            .build();
}

Then create a HttpServiceProxyFactory

@Bean
HttpServiceProxyFactory httpServiceProxyFactory(WebClient webClient) {
    return new HttpServiceProxyFactory(WebClientAdapter.forClient(webClient));
}

Then create HotelClient

@Bean
HotelClient hotelClient(HttpServiceProxyFactory httpServiceProxyFactory) {
    return httpServiceProxyFactory.createClient(HotelClient.class);
}

Your HoltelClient will look like this

public interface HotelClient {

    @PostExchange
    HotelResponse getHotel(@RequestBody HotelRequest request);
    
}

Renamed your HotelService to HotelClient as it is a client and moved your host and base path to baseUrl.

Recommended to move your url string to preperties.

  • Related