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 {

View File

@@ -14,6 +14,7 @@
<modules>
<module>spring-cloud-function-sample</module>
<module>spring-cloud-function-sample-pof</module>
<module>spring-cloud-function-sample-pojo</module>
<module>spring-cloud-function-sample-compiler</module>
<module>spring-cloud-function-sample-task</module>

View File

@@ -0,0 +1,113 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>pof-sample</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-function-sample-pof</name>
<description>Spring Cloud Function Web Support</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<reactor.version>3.0.7.RELEASE</reactor.version>
<spring-cloud-function.version>1.0.0.BUILD-SNAPSHOT</spring-cloud-function.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-web</artifactId>
<version>${spring-cloud-function.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.4.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package functions;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.function.context.FunctionScan;
@FunctionScan
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package functions;
import java.util.function.Function;
public class Greeter implements Function<String, String> {
public String apply(String name) {
return "Hello " + name;
}
}