I am using feign client in an application with spring boot version 3.1.4
I made a configuration definition for the feign client.
Configuration class
@Configuration
@RequiredArgsConstructor
public class FeignConfiguration {
@Bean
public Decoder feignDecoder(ObjectFactory<HttpMessageConverters> messageConverters)
{
//bla bla
}
@Bean
public RequestInterceptor requestInterceptor() {
//bla bla
}
}
Feign Client
@FeignClient(name = "", url = "")
public interface FeignClient {
//some methods
}
If I define it as above, it can find the relevant configurations without defining the configuration. However, I want to define a new feign client and I want this to be a new configuration. I am writing a second feign configuration class for this.
Second Configuration class
@Configuration
@RequiredArgsConstructor
public class SecondFeignConfiguration {
@Bean
public Decoder feignDecoder(ObjectFactory<HttpMessageConverters> messageConverters)
{
//bla bla
}
@Bean
public RequestInterceptor requestInterceptor() {
//bla bla
}
}
Second Feign Client
@FeignClient(name = "", url = "", configuration = SecondFeignConfiguration.class)
public interface SecondFeignClient {
//some methods
}
While I'm waiting for my second feign client requests to work based on the configuration I gave, the feign configuration works based on the configuration (the one I defined first).
In the configurations I made for Feign client
If I do not use the @Configuration annotation, it works as I want and I have to specify configuration = "...configuration" for all feign clients I use.
Why does it happen this way?
According to the documentation:
That means that Spring dynamically creates a separate
ApplicationContextfor each Feign client, which allows isolation between them. By declaring configuration using the@FeignClientannotation, you are telling Spring to use that configuration inside theApplicationContextof that particular client. For that reason, it does not need to be annotated with@Configuration. However, if it is, it will become the default source forfeign.Decoder,feign.Encoder, etc., when specified.To conclude, use
@Configurationon the class containing the default beans offeign.Decoder,feign.Encoder, etc. When there is a need for specific beans, create a separate class, without@Configuration, containing the beans that would override the default ones and declare it as the configuration in the@FeignClientannotation.