Commit 892eea75 by Spencer Gibb

Lazily look up MetricsClientHttpRequestInterceptor in BPP.

fixes gh-912
parent 3f9e76b8
......@@ -14,12 +14,15 @@
package org.springframework.cloud.netflix.metrics;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.actuate.metrics.reader.MetricReader;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
......@@ -73,24 +76,37 @@ public class MetricsInterceptorConfiguration {
}
@Bean
BeanPostProcessor spectatorRestTemplateInterceptorPostProcessor(
final MetricsClientHttpRequestInterceptor interceptor) {
return new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean,
String beanName) {
return bean;
}
BeanPostProcessor spectatorRestTemplateInterceptorPostProcessor() {
return new MetricsInterceptorPostProcessor();
}
private static class MetricsInterceptorPostProcessor implements
BeanPostProcessor, ApplicationContextAware {
private ApplicationContext context;
private MetricsClientHttpRequestInterceptor interceptor;
@Override
public Object postProcessAfterInitialization(Object bean,
String beanName) {
if (bean instanceof RestTemplate) {
((RestTemplate) bean).getInterceptors().add(interceptor);
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
if (bean instanceof RestTemplate) {
if (this.interceptor == null) {
this.interceptor = this.context
.getBean(MetricsClientHttpRequestInterceptor.class);
}
return bean;
((RestTemplate) bean).getInterceptors().add(interceptor);
}
};
return bean;
}
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context = context;
}
}
}
}
......@@ -16,6 +16,8 @@
package org.springframework.cloud.netflix.metrics;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
......@@ -23,12 +25,16 @@ import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory;
import org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactoryTests;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
/**
* @author Spencer Gibb
......@@ -44,15 +50,23 @@ public class MetricsRestTemplateTests extends RibbonClientHttpRequestFactoryTest
@Override
public void requestFactoryIsRibbon() {
ClientHttpRequestFactory requestFactory = this.restTemplate.getRequestFactory();
assertTrue("wrong RequestFactory type: " + requestFactory.getClass(),
requestFactory instanceof InterceptingClientHttpRequestFactory);
assertThat("wrong RequestFactory type: " + requestFactory.getClass(),
requestFactory, is(instanceOf(InterceptingClientHttpRequestFactory.class)));
InterceptingClientHttpRequestFactory intercepting = (InterceptingClientHttpRequestFactory) requestFactory;
Object interceptorsField = ReflectionTestUtils.getField(intercepting,
"interceptors");
assertThat("wrong interceptors type: " + interceptorsField.getClass(), interceptorsField, is(instanceOf(List.class)));
@SuppressWarnings("unchecked")
List<ClientHttpRequestInterceptor> interceptors = (List<ClientHttpRequestInterceptor>) interceptorsField;
assertThat("interceptors is wrong size", interceptors, hasSize(1));
assertThat("wrong interceptor type", interceptors.get(0),
is(instanceOf(MetricsClientHttpRequestInterceptor.class)));
Object realRequestFactory = ReflectionTestUtils.getField(intercepting,
"requestFactory");
assertTrue("wrong RequestFactory type: " + realRequestFactory.getClass(),
realRequestFactory instanceof RibbonClientHttpRequestFactory);
assertThat("wrong RequestFactory type: " + realRequestFactory.getClass(),
realRequestFactory, is(instanceOf(RibbonClientHttpRequestFactory.class)));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment