I am using a SpringClientFactory
to create a Ribbon client with a custom com.netflix.niws.client.http.RestClient
. This RestClient
is wired with a @Bean
that is request scoped (that is, @Scope("request")
). This is all done as a part of the Brixton release train.
RibbonClientSpecification customSpec = new RibbonClientSpecification("default.custom",
new Class<?>[]{CustomConfig.class}); // defines custom RestClient
SpringClientFactory scf = new SpringClientFactory();
scf.setConfigurations(Arrays.asList(customSpec));
This SpringClientFactory
has an ApplicationContext
that it instantiates on its own for a given name
, and this context doesn't have any Scopes registered with it.
I have a workaround that extends the SpringClientFactory
, but it feels a bit clunky, and it makes assumptions regarding the type of BeanFactory
that is available.
class ScopePropagatingSpringClientFactory extends SpringClientFactory {
protected AnnotationConfigApplicationContext createContext(String name) {
AnnotationConfigApplicationContext context = super.createContext(name);
ApplicationContext parentContext = context.getParent();
if (parentContext != null) {
ConfigurableListableBeanFactory parentBF =
(ConfigurableListableBeanFactory) parentContext.getAutowireCapableBeanFactory();
String[] scopes = parentBF.getRegisteredScopeNames();
for(String scope : scopes) {
context.getBeanFactory().registerScope(scope, parentBF.getRegisteredScope(scope));
}
}
return context;
}
}
Has anyone tried using alternative bean scopes (request, session, globalsession) within a SpringClientFactory
?
This one is more directed at the Spring Cloud maintainers: Is there a conscious reason that scopes are not propagated from the parent context to the ones created within the NamedContextFactory
? It seems like it might just be an oversight.