Added conditional on class for web auto configuration; fixes gh-1790

This commit is contained in:
Marcin Grzejszczak
2020-12-01 13:44:12 +01:00
parent 382f4a0da7
commit c82a9be5d5
2 changed files with 18 additions and 0 deletions

View File

@@ -44,6 +44,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
@@ -61,6 +62,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnBean(HttpTracing.class)
@AutoConfigureAfter(TraceHttpAutoConfiguration.class)
@ConditionalOnClass(HandlerInterceptorAdapter.class)
@Import(SpanCustomizingAsyncHandlerInterceptor.class)
public class TraceWebServletAutoConfiguration {

View File

@@ -19,8 +19,10 @@ package org.springframework.cloud.sleuth.instrument.web;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,6 +38,20 @@ public class TraceWebServletAutoConfigurationTests {
TraceHttpAutoConfiguration.class, TraceWebAutoConfiguration.class,
TraceWebServletAutoConfiguration.class));
@Test
public void shouldNotCreateTracedWebBeansWhenServletClassMissing() {
this.contextRunner.withClassLoader(new FilteredClassLoader(HandlerInterceptorAdapter.class)).run((context) -> {
assertThat(context).doesNotHaveBean(TraceWebAspect.class);
});
}
@Test
public void shouldCreateTracedWebBeansWhenServletClassNotMissing() {
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(TraceWebAspect.class);
});
}
@Test
public void shouldNotCreateExceptionLoggingFilterBeanByDefault() {
this.contextRunner.run((context) -> {