Ensure @Component scanned from jar on classpath can be inspected

And test it using a new POF sample.
This commit is contained in:
Dave Syer
2017-06-28 17:11:44 +01:00
parent d1ccef62b5
commit d826884d02
7 changed files with 216 additions and 8 deletions

View File

@@ -59,7 +59,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.type.StandardMethodMetadata;
import org.springframework.core.type.classreading.MethodMetadataReadingVisitor;
import org.springframework.messaging.Message;
@@ -407,7 +407,13 @@ public class ContextFunctionCatalogAutoConfiguration {
.getIntrospectedMethod().getGenericReturnType();
param = extractType(type, paramType, index);
}
else if (source instanceof FileSystemResource) {
else if (source instanceof MethodMetadataReadingVisitor) {
// A component scan with @Beans
MethodMetadataReadingVisitor visitor = (MethodMetadataReadingVisitor) source;
Type type = findBeanType(definition, visitor);
param = extractType(type, paramType, index);
}
else if (source instanceof Resource) {
try {
Class<?> beanType = ClassUtils.forName(definition.getBeanClassName(),
null);
@@ -427,12 +433,6 @@ public class ContextFunctionCatalogAutoConfiguration {
"Cannot instrospect bean: " + definition, e);
}
}
else if (source instanceof MethodMetadataReadingVisitor) {
// A component scan with @Beans
MethodMetadataReadingVisitor visitor = (MethodMetadataReadingVisitor) source;
Type type = findBeanType(definition, visitor);
param = extractType(type, paramType, index);
}
else {
ResolvableType resolvable = (ResolvableType) getField(definition,
"targetType");

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.function.context;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -39,9 +41,12 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -139,6 +144,33 @@ public class ContextFunctionCatalogAutoConfigurationTests {
assertThat(inspector.getInputWrapper("function")).isAssignableFrom(Map.class);
}
@Test
public void componentScanJarFunction() {
try {
create("greeter.jar", ComponentScanJarConfiguration.class);
assertThat(context.getBean("greeter")).isInstanceOf(Function.class);
assertThat(catalog.lookupFunction("greeter")).isInstanceOf(Function.class);
assertThat(inspector.getInputType("greeter")).isAssignableFrom(String.class);
assertThat(inspector.getInputWrapper("greeter"))
.isAssignableFrom(String.class);
}
finally {
ClassUtils.overrideThreadContextClassLoader(getClass().getClassLoader());
}
}
private void create(String jarfile, Class<?> config, String... props) {
try {
URL[] urls = new URL[] { new ClassPathResource(jarfile).getURL() };
ClassUtils.overrideThreadContextClassLoader(
new URLClassLoader(urls, getClass().getClassLoader()));
create(config, props);
}
catch (Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
}
@Test
public void simpleSupplier() {
create(SimpleConfiguration.class);
@@ -304,6 +336,12 @@ public class ContextFunctionCatalogAutoConfigurationTests {
protected static class ComponentScanConfiguration {
}
@EnableAutoConfiguration
@Configuration
@FunctionScan
protected static class ComponentScanJarConfiguration {
}
@EnableAutoConfiguration
@Configuration
protected static class GenericFluxConfiguration {