我正在使用 aSpringClientFactory
创建一个带有自定义的功能区客户端com.netflix.niws.client.http.RestClient
。这RestClient
与@Bean
请求范围内的(即@Scope("request")
)连接。这一切都是作为 Brixton 发布系列的一部分完成的。
RibbonClientSpecification customSpec = new RibbonClientSpecification("default.custom",
new Class<?>[]{CustomConfig.class}); // defines custom RestClient
SpringClientFactory scf = new SpringClientFactory();
scf.setConfigurations(Arrays.asList(customSpec));
这SpringClientFactory
有一个ApplicationContext
它自己为给定实例化的name
,并且这个上下文没有任何注册的范围。
我有一个扩展的解决方法SpringClientFactory
,但感觉有点笨拙,并且它对BeanFactory
可用的类型做出假设。
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;
}
}
有没有人尝试在 a 中使用替代 bean 范围(请求、会话、全局会话)SpringClientFactory
?
这个更针对 Spring Cloud 维护者:是否有意识的原因是范围没有从父上下文传播到在NamedContextFactory
? 似乎这可能只是一个疏忽。