diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/SpringClientFactory.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/SpringClientFactory.java index f3ebd424..e63d49f0 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/SpringClientFactory.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/SpringClientFactory.java @@ -80,29 +80,28 @@ public class SpringClientFactory extends NamedContextFactory C instantiateWithConfig(AnnotationConfigApplicationContext context, - Class clazz, IClientConfig config) { + Class clazz, IClientConfig config) { C result = null; - + try { Constructor constructor = clazz.getConstructor(IClientConfig.class); result = constructor.newInstance(config); - } - catch (Throwable e) { + } catch (Throwable e) { // Ignored } - + if (result == null) { result = BeanUtils.instantiate(clazz); - + if (result instanceof IClientConfigAware) { ((IClientConfigAware) result).initWithNiwsConfig(config); } - + if (context != null) { context.getAutowireCapableBeanFactory().autowireBean(result); } } - + return result; } @@ -120,4 +119,6 @@ public class SpringClientFactory extends NamedContextFactory result = new TestRestTemplate().getForEntity(uri, + String.class); + + // the instance should be available now.. + assertThat(Foo.getInstanceCount()).isEqualTo(1); + + assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(result.getBody()).isEqualTo("sample"); + } + + @EnableAutoConfiguration + @Configuration + @EnableZuulProxy + @RibbonClients(@RibbonClient(name = "eager", configuration = FooConfig.class)) + static class TestConfig { + + } + + static class Foo { + private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger(); + + public Foo() { + INSTANCE_COUNT.incrementAndGet(); + } + + public static int getInstanceCount() { + return INSTANCE_COUNT.get(); + } + } + + static class FooConfig { + + @Bean + public Foo foo() { + return new Foo(); + } + + @Value("${local.server.port}") + private int port; + + @Bean + public ServerList ribbonServerList() { + return new StaticServerList<>(new Server("localhost", this.port)); + } + + } + + @Configuration + @RestController + static class SampleWebConfig { + + @RequestMapping("/sample") + public String sampleEndpoint() { + return "sample"; + } + + } +}