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

The contexts in which clients are created contain no clues about the client being created #1131

Open
lildadou opened this issue Nov 26, 2024 · 0 comments

Comments

@lildadou
Copy link

My goal

I want to be able to declare a feign.Client bean (the http one) for each feign client. Furthermore, I want these feign.Client to be customized for each feign client.

How I achieved this

The feign.Client is retrieved/produced by FeignClientFactoryBean@L486.

Client client = getOptional(feignClientFactory, Client.class);

If I have a configuration that produces feign.Client in the context used by FeignClientFactoryBean then I will be able to control the feign.Client retrieved by FeignClientFactoryBean.
EnableFeignClients#defaultConfiguration allows to do this so I declared this:

@Bean
@Scope("prototype")
public feign.Client feignHttpClient() {
	return internalBuildFeignHttpClient("todo");
}

As I need to know the client I am building so I add some injections:

@Bean
@Scope("prototype")
public feign.Client feignHttpClient(
            FeignClientFactoryBean feignClientFactoryBean,
            FeignClientSpecification feignClientSpecification) {
	final String clientName = feignClientFactoryBean.getName();
	return internalBuildFeignHttpClient(clientName);
}

The problem

The problem with this code is that clientName is always equal to the name of the first Feign client instantiated. This is because there is no FeignClientFactoryBean in the current context (nor any way to know which Feign client is being built).

My suggestion

So, I propose to put in the current context the FeignClientFactoryBean which uses it.

A temporary solution

For those who have the same use case as me, you can find the name of the client being built with this not very elegant trick:

@Bean
@Scope("prototype")
public feign.Client feignHttpClient(
            FeignClientFactoryBean feignClientFactoryBean,
            FeignClientSpecification feignClientSpecification,
            GenericApplicationContext genericApplicationContext) {
	// final String clientName = feignClientFactoryBean.getName();
        final String clientName = genericApplicationContext.getDisplayName().substring(FeignClientFactory.class.getSimpleName().length() + 1);
	return internalBuildFeignHttpClient(clientName);
}
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