下面列出了org.springframework.context.ConfigurableApplicationContext#getBeanNamesForType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@DataProvider(value = {
"MANUAL_IMPORT_ONLY",
"COMPONENT_SCAN_ONLY",
"BOTH_MANUAL_AND_COMPONENT_SCAN"
})
@Test
public void component_test(ComponentTestSetup componentTestSetup) {
// given
int serverPort = findFreePort();
Class<?> mainClass = componentTestSetup.mainClass;
ConfigurableApplicationContext serverAppContext = SpringApplication.run(mainClass, "--server.port=" + serverPort);
try {
// when
WingtipsSpringBootConfiguration config = serverAppContext.getBean(WingtipsSpringBootConfiguration.class);
WingtipsSpringBootProperties props = serverAppContext.getBean(WingtipsSpringBootProperties.class);
String[] someComponentScannedClassBeanNames =
serverAppContext.getBeanNamesForType(SomeComponentScannedClass.class);
// then
// Sanity check that we component scanned (or not) as appropriate.
if (componentTestSetup.expectComponentScannedObjects) {
assertThat(someComponentScannedClassBeanNames).isNotEmpty();
}
else {
assertThat(someComponentScannedClassBeanNames).isEmpty();
}
// WingtipsSpringBootConfiguration and WingtipsSpringBootProperties should be available as beans, and
// the config should use the same props we received.
assertThat(config).isNotNull();
assertThat(props).isNotNull();
assertThat(config.wingtipsProperties).isSameAs(props);
}
finally {
SpringApplication.exit(serverAppContext);
}
}
@DataProvider(value = {
"MANUAL_IMPORT_ONLY",
"COMPONENT_SCAN_ONLY",
"BOTH_MANUAL_AND_COMPONENT_SCAN",
"WITH_ZIPKIN_REPORTER_OVERRIDE",
"WITH_CONVERTER_OVERRIDE",
"WITH_BOTH_REPORTER_AND_CONVERTER_OVERRIDES"
})
@Test
public void component_test(ComponentTestSetup componentTestSetup) {
// given
int serverPort = findFreePort();
Class<?> mainClass = componentTestSetup.mainClass;
ConfigurableApplicationContext serverAppContext = SpringApplication.run(mainClass, "--server.port=" + serverPort);
try {
// when
WingtipsSpringBoot2WebfluxConfiguration
baseConfig = serverAppContext.getBean(WingtipsSpringBoot2WebfluxConfiguration.class);
WingtipsWithZipkinSpringBoot2WebfluxConfiguration zipkinConfig =
serverAppContext.getBean(WingtipsWithZipkinSpringBoot2WebfluxConfiguration.class);
WingtipsSpringBoot2WebfluxProperties
baseProps = serverAppContext.getBean(WingtipsSpringBoot2WebfluxProperties.class);
WingtipsZipkinProperties zipkinProps = serverAppContext.getBean(WingtipsZipkinProperties.class);
String[] someComponentScannedClassBeanNames =
serverAppContext.getBeanNamesForType(SomeComponentScannedClass.class);
List<SpanLifecycleListener> lifecycleListeners = Tracer.getInstance().getSpanLifecycleListeners();
// then
// Sanity check that we component scanned (or not) as appropriate.
if (componentTestSetup.expectComponentScannedObjects) {
assertThat(someComponentScannedClassBeanNames).isNotEmpty();
}
else {
assertThat(someComponentScannedClassBeanNames).isEmpty();
}
// WingtipsSpringBoot2WebfluxConfiguration, WingtipsWithZipkinSpringBoot2WebfluxConfiguration,
// WingtipsSpringBoot2WebfluxProperties, and WingtipsZipkinProperties should be available as beans, and
// the base config should use the same props we received.
assertThat(baseConfig).isNotNull();
assertThat(baseProps).isNotNull();
assertThat(baseConfig).extracting("wingtipsProperties")
.containsExactly(baseProps);
assertThat(zipkinConfig).isNotNull();
assertThat(zipkinProps).isNotNull();
// Verify that a WingtipsToZipkinLifecycleListener was registered with Tracer.
assertThat(lifecycleListeners).hasSize(1);
SpanLifecycleListener listener = lifecycleListeners.get(0);
assertThat(listener).isInstanceOf(WingtipsToZipkinLifecycleListener.class);
// Verify the Zipkin Reporter override if expected.
if (componentTestSetup.expectedReporterOverride != null) {
assertThat(Whitebox.getInternalState(listener, "zipkinSpanReporter"))
.isSameAs(ComponentTestMainWithReporterOverride.CUSTOM_REPORTER_INSTANCE);
}
// Verify the Wingtips-to-Zipkin converter override if expected.
if (componentTestSetup.expectedConverterOverride != null) {
assertThat(Whitebox.getInternalState(listener, "zipkinSpanConverter"))
.isSameAs(ComponentTestMainWithConverterOverride.CUSTOM_CONVERTER_INSTANCE);
}
}
finally {
SpringApplication.exit(serverAppContext);
}
}
@DataProvider(value = {
"MANUAL_IMPORT_ONLY",
"COMPONENT_SCAN_ONLY",
"BOTH_MANUAL_AND_COMPONENT_SCAN"
})
@Test
public void component_test(ComponentTestSetup componentTestSetup) {
// given
int serverPort = findFreePort();
Class<?> mainClass = componentTestSetup.mainClass;
ConfigurableApplicationContext serverAppContext = SpringApplication.run(mainClass,
"--server.port=" + serverPort);
try {
// when
WingtipsSpringBoot2WebfluxConfiguration
config = serverAppContext.getBean(WingtipsSpringBoot2WebfluxConfiguration.class);
WingtipsSpringBoot2WebfluxProperties props =
serverAppContext.getBean(WingtipsSpringBoot2WebfluxProperties.class);
String[] someComponentScannedClassBeanNames =
serverAppContext.getBeanNamesForType(SomeComponentScannedClass.class);
// then
// Sanity check that we component scanned (or not) as appropriate.
if (componentTestSetup.expectComponentScannedObjects) {
assertThat(someComponentScannedClassBeanNames).isNotEmpty();
} else {
assertThat(someComponentScannedClassBeanNames).isEmpty();
}
// WingtipsSpringBoot2WebfluxConfiguration and WingtipsSpringBoot2WebfluxProperties should be available as
// beans, and the config should use the same props we received.
assertThat(config).isNotNull();
assertThat(props).isNotNull();
assertThat(config.wingtipsProperties).isSameAs(props);
// The config should not have any custom WingtipsSpringWebfluxWebFilter. Spring will populate
// the config.customSpringWebfluxWebFilter field with whatever wingtipsSpringWebfluxWebFilter()
// produces - so they should be the same.
Map<String, WingtipsSpringWebfluxWebFilter> filtersFromSpring =
serverAppContext.getBeansOfType(WingtipsSpringWebfluxWebFilter.class);
assertThat(filtersFromSpring).isEqualTo(
Collections.singletonMap("wingtipsSpringWebfluxWebFilter", config.customSpringWebfluxWebFilter)
);
} finally {
Schedulers.removeExecutorServiceDecorator(WingtipsReactorInitializer.WINGTIPS_SCHEDULER_KEY);
SpringApplication.exit(serverAppContext);
}
}
@Test
public void component_test_with_custom_WingtipsSpringWebfluxWebFilter() {
// given
int serverPort = findFreePort();
ConfigurableApplicationContext serverAppContext = SpringApplication.run(
ComponentTestMainWithCustomWingtipsWebFilter.class,
"--server.port=" + serverPort
);
try {
// when
WingtipsSpringBoot2WebfluxConfiguration
config = serverAppContext.getBean(WingtipsSpringBoot2WebfluxConfiguration.class);
WingtipsSpringBoot2WebfluxProperties props =
serverAppContext.getBean(WingtipsSpringBoot2WebfluxProperties.class);
String[] someComponentScannedClassBeanNames =
serverAppContext.getBeanNamesForType(SomeComponentScannedClass.class);
// then
// Sanity check that we component scanned (or not) as appropriate. This particular component test does
// include component scanning.
assertThat(someComponentScannedClassBeanNames).isNotEmpty();
// WingtipsSpringBoot2WebfluxConfiguration and WingtipsSpringBoot2WebfluxProperties should be available as
// beans, and the config should use the same props we received.
assertThat(config).isNotNull();
assertThat(props).isNotNull();
assertThat(config.wingtipsProperties).isSameAs(props);
// Finally, the thing this test is verifying: the config's custom filter should be the same one from the
// component test main class, and it should be the one that Spring exposes.
assertThat(config.customSpringWebfluxWebFilter)
.isSameAs(ComponentTestMainWithCustomWingtipsWebFilter.customFilter);
Map<String, WingtipsSpringWebfluxWebFilter> filtersFromSpring =
serverAppContext.getBeansOfType(WingtipsSpringWebfluxWebFilter.class);
assertThat(filtersFromSpring).isEqualTo(
Collections.singletonMap("customFilter", ComponentTestMainWithCustomWingtipsWebFilter.customFilter)
);
} finally {
SpringApplication.exit(serverAppContext);
}
}
@DataProvider(value = {
"MANUAL_IMPORT_ONLY",
"COMPONENT_SCAN_ONLY",
"BOTH_MANUAL_AND_COMPONENT_SCAN",
"WITH_ZIPKIN_REPORTER_OVERRIDE",
"WITH_CONVERTER_OVERRIDE",
"WITH_BOTH_REPORTER_AND_CONVERTER_OVERRIDES"
})
@Test
public void component_test(ComponentTestSetup componentTestSetup) {
// given
int serverPort = findFreePort();
Class<?> mainClass = componentTestSetup.mainClass;
ConfigurableApplicationContext serverAppContext = SpringApplication.run(mainClass, "--server.port=" + serverPort);
try {
// when
WingtipsSpringBootConfiguration baseConfig = serverAppContext.getBean(WingtipsSpringBootConfiguration.class);
WingtipsWithZipkinSpringBootConfiguration zipkinConfig =
serverAppContext.getBean(WingtipsWithZipkinSpringBootConfiguration.class);
WingtipsSpringBootProperties baseProps = serverAppContext.getBean(WingtipsSpringBootProperties.class);
WingtipsZipkinProperties zipkinProps = serverAppContext.getBean(WingtipsZipkinProperties.class);
String[] someComponentScannedClassBeanNames =
serverAppContext.getBeanNamesForType(SomeComponentScannedClass.class);
List<SpanLifecycleListener> lifecycleListeners = Tracer.getInstance().getSpanLifecycleListeners();
// then
// Sanity check that we component scanned (or not) as appropriate.
if (componentTestSetup.expectComponentScannedObjects) {
assertThat(someComponentScannedClassBeanNames).isNotEmpty();
}
else {
assertThat(someComponentScannedClassBeanNames).isEmpty();
}
// WingtipsSpringBootConfiguration, WingtipsWithZipkinSpringBootConfiguration,
// WingtipsSpringBootProperties, and WingtipsZipkinProperties should be available as beans, and
// the base config should use the same props we received.
assertThat(baseConfig).isNotNull();
assertThat(baseProps).isNotNull();
assertThat(baseConfig).extracting("wingtipsProperties")
.containsExactly(baseProps);
assertThat(zipkinConfig).isNotNull();
assertThat(zipkinProps).isNotNull();
// Verify that a WingtipsToZipkinLifecycleListener was registered with Tracer.
assertThat(lifecycleListeners).hasSize(1);
SpanLifecycleListener listener = lifecycleListeners.get(0);
assertThat(listener).isInstanceOf(WingtipsToZipkinLifecycleListener.class);
// Verify the Zipkin Reporter override if expected.
if (componentTestSetup.expectedReporterOverride != null) {
assertThat(Whitebox.getInternalState(listener, "zipkinSpanReporter"))
.isSameAs(ComponentTestMainWithReporterOverride.CUSTOM_REPORTER_INSTANCE);
}
// Verify the Wingtips-to-Zipkin converter override if expected.
if (componentTestSetup.expectedConverterOverride != null) {
assertThat(Whitebox.getInternalState(listener, "zipkinSpanConverter"))
.isSameAs(ComponentTestMainWithConverterOverride.CUSTOM_CONVERTER_INSTANCE);
}
}
finally {
SpringApplication.exit(serverAppContext);
}
}