spring - How to dynamically set @Bean at @Configuration in @Service? -
i use spring cloud & feign client app.and want set param 'accept-language' headers when call feign clients. found similar questions @ [using @headers dynamic values in feign client + spring cloud (brixton rc2)
ask]1,but don't know how set header param.here code: set mydefaultfeignconfig @ app.java @enablefeignclients(basepackages = {defaultconfiguration = mydefaultfeignconfig.class)
and mydefaultfeignconfig.java :
@configuration public class mydefaultfeignconfig { private string requestlanguage = "zh"; @bean requestinterceptor feignrequestinterceptor() { return new requestinterceptor() { @override public void apply(requesttemplate template) { template.header("accept-language", requestlanguage); } }; } //doesn't work public static void updatebean(string requestlanguage) { annotationconfigapplicationcontext applicationcontext = new annotationconfigapplicationcontext(mydefaultfeignconfig.class); try { system.out.println(applicationcontext.getbean("feignrequestinterceptor")); } catch (nosuchbeandefinitionexception e) { system.out.println("bean not found"); } beandefinitionregistry beanfactory = (beandefinitionregistry) applicationcontext.getbeanfactory(); beanfactory.registerbeandefinition("feignrequestinterceptor", beandefinitionbuilder.genericbeandefinition(string.class) .addconstructorargvalue(new requestinterceptor() { @override public void apply(requesttemplate template) { template.header("accept-language", requestlanguage); } }) .getbeandefinition() ); } }
my gateway controller :
@autowired private leaseorderremoteservice leaseorderremoteservice; @requestmapping(value = "/discovery/order/unifiyinit", method = requestmethod.get) public message unifiyorderinit(@requestheader("accept-language") string language) { mydefaultfeignconfig.updatebean(language); return leaseorderremoteservice.unifiyorderinit(); }
my feign clients controller is:
public message unifiyorderinit(@requestheader("accept-language") string language) { //... }
and can value of "accept-language" mydefaultfeignconfig config first time set @bean.how can set value of "accept-language" gateway feign client.please me,thinks! suggestions grateful , best regards!
Comments
Post a Comment