Olga Maciaszek-Sharma
2 years ago
committed by
GitHub
38 changed files with 931 additions and 105 deletions
@ -0,0 +1,126 @@
@@ -0,0 +1,126 @@
|
||||
/* |
||||
* Copyright 2022-2022 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 |
||||
* |
||||
* https://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 org.springframework.cloud.openfeign.aot; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import javax.lang.model.element.Modifier; |
||||
|
||||
import org.springframework.aot.generate.GeneratedMethod; |
||||
import org.springframework.aot.generate.GenerationContext; |
||||
import org.springframework.beans.factory.BeanFactory; |
||||
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; |
||||
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; |
||||
import org.springframework.beans.factory.aot.BeanRegistrationCode; |
||||
import org.springframework.beans.factory.support.RegisteredBean; |
||||
import org.springframework.cloud.openfeign.FeignClientFactory; |
||||
import org.springframework.cloud.openfeign.FeignClientSpecification; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.context.aot.ApplicationContextAotGenerator; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.javapoet.ClassName; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* A {@link BeanRegistrationAotProcessor} that creates an |
||||
* {@link BeanRegistrationAotContribution} for Feign child contexts. |
||||
* |
||||
* @author Olga Maciaszek-Sharma |
||||
* @since 4.0.0 |
||||
*/ |
||||
public class FeignChildContextInitializer implements BeanRegistrationAotProcessor { |
||||
|
||||
private final ApplicationContext applicationContext; |
||||
|
||||
private final FeignClientFactory feignClientFactory; |
||||
|
||||
public FeignChildContextInitializer(ApplicationContext applicationContext, FeignClientFactory feignClientFactory) { |
||||
this.applicationContext = applicationContext; |
||||
this.feignClientFactory = feignClientFactory; |
||||
} |
||||
|
||||
@Override |
||||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { |
||||
Assert.isInstanceOf(ConfigurableApplicationContext.class, applicationContext); |
||||
ConfigurableApplicationContext context = ((ConfigurableApplicationContext) applicationContext); |
||||
BeanFactory applicationBeanFactory = context.getBeanFactory(); |
||||
if (!((registeredBean.getBeanClass().equals(FeignClientFactory.class)) |
||||
&& registeredBean.getBeanFactory().equals(applicationBeanFactory))) { |
||||
return null; |
||||
} |
||||
Set<String> contextIds = new HashSet<>(getContextIdsFromConfig()); |
||||
Map<String, GenericApplicationContext> childContextAotContributions = contextIds.stream() |
||||
.map(contextId -> Map.entry(contextId, buildChildContext(contextId))) |
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
||||
return new AotContribution(childContextAotContributions); |
||||
} |
||||
|
||||
private GenericApplicationContext buildChildContext(String contextId) { |
||||
GenericApplicationContext childContext = feignClientFactory.buildContext(contextId); |
||||
feignClientFactory.registerBeans(contextId, childContext); |
||||
return childContext; |
||||
} |
||||
|
||||
private Collection<String> getContextIdsFromConfig() { |
||||
Map<String, FeignClientSpecification> configurations = feignClientFactory.getConfigurations(); |
||||
return configurations.keySet().stream().filter(key -> !key.startsWith("default.")).collect(Collectors.toSet()); |
||||
} |
||||
|
||||
private static class AotContribution implements BeanRegistrationAotContribution { |
||||
|
||||
private final Map<String, GenericApplicationContext> childContexts; |
||||
|
||||
AotContribution(Map<String, GenericApplicationContext> childContexts) { |
||||
this.childContexts = childContexts.entrySet().stream().filter(entry -> entry.getValue() != null) |
||||
.map(entry -> Map.entry(entry.getKey(), entry.getValue())) |
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
||||
} |
||||
|
||||
@Override |
||||
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) { |
||||
Map<String, ClassName> generatedInitializerClassNames = childContexts.entrySet().stream().map(entry -> { |
||||
String name = entry.getValue().getDisplayName(); |
||||
name = name.replaceAll("[-]", "_"); |
||||
GenerationContext childGenerationContext = generationContext.withName(name); |
||||
ClassName initializerClassName = new ApplicationContextAotGenerator() |
||||
.processAheadOfTime(entry.getValue(), childGenerationContext); |
||||
return Map.entry(entry.getKey(), initializerClassName); |
||||
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
||||
GeneratedMethod postProcessorMethod = beanRegistrationCode.getMethods() |
||||
.add("addFeignChildContextInitializer", method -> { |
||||
method.addJavadoc("Use AOT child context management initialization") |
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC) |
||||
.addParameter(RegisteredBean.class, "registeredBean") |
||||
.addParameter(FeignClientFactory.class, "instance").returns(FeignClientFactory.class) |
||||
.addStatement("$T<String, Object> initializers = new $T<>()", Map.class, HashMap.class); |
||||
generatedInitializerClassNames.keySet() |
||||
.forEach(contextId -> method.addStatement("initializers.put($S, new $L())", contextId, |
||||
generatedInitializerClassNames.get(contextId))); |
||||
method.addStatement("return instance.withApplicationContextInitializers(initializers)"); |
||||
}); |
||||
beanRegistrationCode.addInstancePostProcessor(postProcessorMethod.toMethodReference()); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,182 @@
@@ -0,0 +1,182 @@
|
||||
/* |
||||
* Copyright 2022-2022 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 |
||||
* |
||||
* https://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 org.springframework.cloud.openfeign.aot; |
||||
|
||||
import java.util.Map; |
||||
import java.util.Objects; |
||||
import java.util.Set; |
||||
import java.util.stream.Collectors; |
||||
|
||||
import javax.lang.model.element.Modifier; |
||||
|
||||
import org.springframework.aot.generate.GenerationContext; |
||||
import org.springframework.aot.generate.MethodReference; |
||||
import org.springframework.aot.hint.ProxyHints; |
||||
import org.springframework.beans.MutablePropertyValues; |
||||
import org.springframework.beans.factory.BeanFactory; |
||||
import org.springframework.beans.factory.FactoryBean; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; |
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; |
||||
import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter; |
||||
import org.springframework.beans.factory.config.BeanDefinition; |
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder; |
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition; |
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; |
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
||||
import org.springframework.beans.factory.support.GenericBeanDefinition; |
||||
import org.springframework.beans.factory.support.RegisteredBean; |
||||
import org.springframework.cloud.openfeign.FeignClientFactory; |
||||
import org.springframework.cloud.openfeign.FeignClientFactoryBean; |
||||
import org.springframework.cloud.openfeign.FeignClientSpecification; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.javapoet.MethodSpec; |
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* A {@link BeanFactoryInitializationAotProcessor} that creates an |
||||
* {@link BeanFactoryInitializationAotContribution} that registers bean definitions and |
||||
* proxy hints for Feign client beans. |
||||
* |
||||
* @author Olga Maciaszek-Sharma |
||||
* @since 4.0.0 |
||||
*/ |
||||
public class FeignClientBeanFactoryInitializationAotProcessor |
||||
implements BeanRegistrationExcludeFilter, BeanFactoryInitializationAotProcessor { |
||||
|
||||
private final GenericApplicationContext context; |
||||
|
||||
private final Map<String, BeanDefinition> feignClientBeanDefinitions; |
||||
|
||||
public FeignClientBeanFactoryInitializationAotProcessor(GenericApplicationContext context, |
||||
FeignClientFactory feignClientFactory) { |
||||
this.context = context; |
||||
this.feignClientBeanDefinitions = getFeignClientBeanDefinitions(feignClientFactory); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) { |
||||
return registeredBean.getBeanClass().equals(FeignClientFactoryBean.class) |
||||
|| feignClientBeanDefinitions.containsKey(registeredBean.getBeanClass().getName()); |
||||
} |
||||
|
||||
private Map<String, BeanDefinition> getFeignClientBeanDefinitions(FeignClientFactory feignClientFactory) { |
||||
Map<String, FeignClientSpecification> configurations = feignClientFactory.getConfigurations(); |
||||
return configurations.values().stream().map(FeignClientSpecification::getClassName).filter(Objects::nonNull) |
||||
.filter(className -> !className.equals("default")) |
||||
.map(className -> Map.entry(className, context.getBeanDefinition(className))) |
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
||||
} |
||||
|
||||
@Override |
||||
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { |
||||
BeanFactory applicationBeanFactory = context.getBeanFactory(); |
||||
if (feignClientBeanDefinitions.isEmpty() || !beanFactory.equals(applicationBeanFactory)) { |
||||
return null; |
||||
} |
||||
return new AotContribution(feignClientBeanDefinitions); |
||||
} |
||||
|
||||
private static final class AotContribution implements BeanFactoryInitializationAotContribution { |
||||
|
||||
private final Map<String, BeanDefinition> feignClientBeanDefinitions; |
||||
|
||||
private AotContribution(Map<String, BeanDefinition> feignClientBeanDefinitions) { |
||||
this.feignClientBeanDefinitions = feignClientBeanDefinitions; |
||||
} |
||||
|
||||
@Override |
||||
public void applyTo(GenerationContext generationContext, |
||||
BeanFactoryInitializationCode beanFactoryInitializationCode) { |
||||
ProxyHints proxyHints = generationContext.getRuntimeHints().proxies(); |
||||
Set<String> feignClientRegistrationMethods = feignClientBeanDefinitions.values().stream() |
||||
.map(beanDefinition -> { |
||||
Assert.notNull(beanDefinition, "beanDefinition cannot be null"); |
||||
Assert.isInstanceOf(GenericBeanDefinition.class, beanDefinition); |
||||
GenericBeanDefinition registeredBeanDefinition = (GenericBeanDefinition) beanDefinition; |
||||
MutablePropertyValues feignClientProperties = registeredBeanDefinition.getPropertyValues(); |
||||
String className = (String) feignClientProperties.get("type"); |
||||
Assert.notNull(className, "className cannot be null"); |
||||
Class clazz = ClassUtils.resolveClassName(className, null); |
||||
proxyHints.registerJdkProxy(clazz); |
||||
return beanFactoryInitializationCode.getMethods() |
||||
.add(buildMethodName(className), method -> generateFeignClientRegistrationMethod(method, |
||||
feignClientProperties, registeredBeanDefinition)) |
||||
.getName(); |
||||
}).collect(Collectors.toSet()); |
||||
MethodReference initializerMethod = beanFactoryInitializationCode.getMethods() |
||||
.add("initialize", method -> generateInitializerMethod(method, feignClientRegistrationMethods)) |
||||
.toMethodReference(); |
||||
beanFactoryInitializationCode.addInitializer(initializerMethod); |
||||
} |
||||
|
||||
private String buildMethodName(String clientName) { |
||||
return "register" + clientName + "FeignClient"; |
||||
} |
||||
|
||||
private void generateInitializerMethod(MethodSpec.Builder method, Set<String> feignClientRegistrationMethods) { |
||||
method.addModifiers(Modifier.PUBLIC); |
||||
method.addParameter(DefaultListableBeanFactory.class, "registry"); |
||||
feignClientRegistrationMethods.forEach(feignClientRegistrationMethod -> method.addStatement("$N(registry)", |
||||
feignClientRegistrationMethod)); |
||||
} |
||||
|
||||
private void generateFeignClientRegistrationMethod(MethodSpec.Builder method, |
||||
MutablePropertyValues feignClientPropertyValues, GenericBeanDefinition registeredBeanDefinition) { |
||||
Object feignQualifiers = feignClientPropertyValues.get("qualifiers"); |
||||
Assert.notNull(feignQualifiers, "Feign qualifiers cannot be null"); |
||||
String qualifiers = "{\"" + String.join("\",\"", (String[]) feignQualifiers) + "\"}"; |
||||
method.addJavadoc("register Feign Client: $L", feignClientPropertyValues.get("type")) |
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC) |
||||
.addParameter(BeanDefinitionRegistry.class, "registry") |
||||
.addStatement("Class clazz = $T.resolveClassName(\"$L\", null)", ClassUtils.class, |
||||
feignClientPropertyValues.get("type")) |
||||
.addStatement("$T definition = $T.genericBeanDefinition($T.class)", BeanDefinitionBuilder.class, |
||||
BeanDefinitionBuilder.class, FeignClientFactoryBean.class) |
||||
.addStatement("definition.addPropertyValue(\"name\",\"$L\")", feignClientPropertyValues.get("name")) |
||||
.addStatement("definition.addPropertyValue(\"contextId\", \"$L\")", |
||||
feignClientPropertyValues.get("contextId")) |
||||
.addStatement("definition.addPropertyValue(\"type\", clazz)") |
||||
.addStatement("definition.addPropertyValue(\"url\", \"$L\")", feignClientPropertyValues.get("url")) |
||||
.addStatement("definition.addPropertyValue(\"path\", \"$L\")", |
||||
feignClientPropertyValues.get("path")) |
||||
.addStatement("definition.addPropertyValue(\"dismiss404\", $L)", |
||||
feignClientPropertyValues.get("dismiss404")) |
||||
.addStatement("definition.addPropertyValue(\"fallback\", $T.class)", |
||||
feignClientPropertyValues.get("fallback")) |
||||
.addStatement("definition.addPropertyValue(\"fallbackFactory\", $T.class)", |
||||
feignClientPropertyValues.get("fallbackFactory")) |
||||
.addStatement("definition.setAutowireMode($L)", registeredBeanDefinition.getAutowireMode()) |
||||
.addStatement("definition.setLazyInit($L)", |
||||
registeredBeanDefinition.getLazyInit() != null ? registeredBeanDefinition.getLazyInit() |
||||
: false) |
||||
.addStatement("$T beanDefinition = definition.getBeanDefinition()", AbstractBeanDefinition.class) |
||||
.addStatement("beanDefinition.setAttribute(\"$L\", clazz)", FactoryBean.OBJECT_TYPE_ATTRIBUTE) |
||||
.addStatement("beanDefinition.setPrimary($L)", registeredBeanDefinition.isPrimary()) |
||||
.addStatement("$T holder = new $T(beanDefinition, \"$L\", new String[]$L)", |
||||
BeanDefinitionHolder.class, BeanDefinitionHolder.class, |
||||
feignClientPropertyValues.get("type"), qualifiers) |
||||
.addStatement("$T.registerBeanDefinition(holder, registry) ", BeanDefinitionReaderUtils.class); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,2 @@
@@ -0,0 +1,2 @@
|
||||
org.springframework.aot.hint.RuntimeHintsRegistrar=\ |
||||
org.springframework.cloud.openfeign.FeignHints |
@ -0,0 +1,169 @@
@@ -0,0 +1,169 @@
|
||||
/* |
||||
* Copyright 2013-2022 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 |
||||
* |
||||
* https://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 org.springframework.cloud.openfeign; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.util.List; |
||||
|
||||
import feign.Capability; |
||||
import feign.Feign; |
||||
import feign.Logger; |
||||
import feign.RequestInterceptor; |
||||
import feign.micrometer.MicrometerObservationCapability; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.cloud.openfeign.clientconfig.FeignClientConfigurer; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.util.ReflectionUtils; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* @author matt king |
||||
* @author Jonatan Ivanov |
||||
* @author Olga Maciaszek-Sharma |
||||
*/ |
||||
@DirtiesContext |
||||
@SpringBootTest(classes = EagerInitFeignClientUsingConfigurerTests.Application.class, value = { |
||||
"spring.cloud.openfeign.client.config.default.loggerLevel=full", |
||||
"spring.cloud.openfeign.client.config.default.requestInterceptors[0]=org.springframework.cloud.openfeign.FeignClientUsingPropertiesTests.FooRequestInterceptor", |
||||
"spring.cloud.openfeign.client.config.default.requestInterceptors[1]=org.springframework.cloud.openfeign.FeignClientUsingPropertiesTests.BarRequestInterceptor" }) |
||||
class EagerInitFeignClientUsingConfigurerTests { |
||||
|
||||
@Autowired |
||||
private ApplicationContext applicationContext; |
||||
|
||||
@Autowired |
||||
private FeignClientFactory context; |
||||
|
||||
private static final String BEAN_NAME_PREFIX = "&org.springframework.cloud.openfeign.EagerInitFeignClientUsingConfigurerTests$"; |
||||
|
||||
@Test |
||||
public void testFeignClient() { |
||||
FeignClientFactoryBean factoryBean = (FeignClientFactoryBean) applicationContext |
||||
.getBean(BEAN_NAME_PREFIX + "TestFeignClient"); |
||||
Feign.Builder builder = factoryBean.feign(context); |
||||
|
||||
List<RequestInterceptor> interceptors = (List) getBuilderValue(builder, "requestInterceptors"); |
||||
assertThat(interceptors.size()).as("interceptors not set").isEqualTo(3); |
||||
assertThat(getBuilderValue(builder, "logLevel")).as("log level not set").isEqualTo(Logger.Level.FULL); |
||||
|
||||
List<Capability> capabilities = (List) getBuilderValue(builder, "capabilities"); |
||||
assertThat(capabilities).hasSize(2).hasAtLeastOneElementOfType(NoOpCapability.class) |
||||
.hasAtLeastOneElementOfType(MicrometerObservationCapability.class); |
||||
} |
||||
|
||||
private Object getBuilderValue(Feign.Builder builder, String member) { |
||||
Field builderField = ReflectionUtils.findField(Feign.Builder.class, member); |
||||
ReflectionUtils.makeAccessible(builderField); |
||||
|
||||
return ReflectionUtils.getField(builderField, builder); |
||||
} |
||||
|
||||
@Test |
||||
public void testNoInheritFeignClient() { |
||||
FeignClientFactoryBean factoryBean = (FeignClientFactoryBean) applicationContext |
||||
.getBean(BEAN_NAME_PREFIX + "NoInheritFeignClient"); |
||||
Feign.Builder builder = factoryBean.feign(context); |
||||
|
||||
List<RequestInterceptor> interceptors = (List) getBuilderValue(builder, "requestInterceptors"); |
||||
assertThat(interceptors).as("interceptors not set").isEmpty(); |
||||
assertThat(factoryBean.isInheritParentContext()).as("is inheriting from parent configuration").isFalse(); |
||||
|
||||
List<Capability> capabilities = (List) getBuilderValue(builder, "capabilities"); |
||||
assertThat(capabilities).hasSize(2).hasAtLeastOneElementOfType(NoOpCapability.class) |
||||
.hasAtLeastOneElementOfType(MicrometerObservationCapability.class); |
||||
} |
||||
|
||||
@Test |
||||
public void testNoInheritFeignClient_ignoreProperties() { |
||||
FeignClientFactoryBean factoryBean = (FeignClientFactoryBean) applicationContext |
||||
.getBean(BEAN_NAME_PREFIX + "NoInheritFeignClient"); |
||||
Feign.Builder builder = factoryBean.feign(context); |
||||
|
||||
assertThat(getBuilderValue(builder, "logLevel")).as("log level not set").isEqualTo(Logger.Level.HEADERS); |
||||
|
||||
List<Capability> capabilities = (List) getBuilderValue(builder, "capabilities"); |
||||
assertThat(capabilities).hasSize(2).hasAtLeastOneElementOfType(NoOpCapability.class) |
||||
.hasAtLeastOneElementOfType(MicrometerObservationCapability.class); |
||||
} |
||||
|
||||
@EnableAutoConfiguration |
||||
@Configuration(proxyBeanMethods = false) |
||||
@EnableFeignClients(clients = { TestFeignClient.class, NoInheritFeignClient.class }) |
||||
protected static class Application { |
||||
|
||||
@Bean |
||||
public RequestInterceptor requestInterceptor() { |
||||
return requestTemplate -> { |
||||
}; |
||||
} |
||||
|
||||
@Bean |
||||
public NoOpCapability noOpCapability() { |
||||
return new NoOpCapability(); |
||||
} |
||||
|
||||
} |
||||
|
||||
public static class NoInheritConfiguration { |
||||
|
||||
@Bean |
||||
public Logger.Level logLevel() { |
||||
return Logger.Level.HEADERS; |
||||
} |
||||
|
||||
@Bean |
||||
public NoOpCapability noOpCapability() { |
||||
return new NoOpCapability(); |
||||
} |
||||
|
||||
@Bean |
||||
public FeignClientConfigurer feignClientConfigurer() { |
||||
return new FeignClientConfigurer() { |
||||
|
||||
@Override |
||||
public boolean inheritParentConfiguration() { |
||||
return false; |
||||
} |
||||
}; |
||||
|
||||
} |
||||
|
||||
} |
||||
|
||||
@FeignClient("testFeignClient") |
||||
interface TestFeignClient { |
||||
|
||||
} |
||||
|
||||
@FeignClient(name = "noInheritFeignClient", configuration = NoInheritConfiguration.class) |
||||
interface NoInheritFeignClient { |
||||
|
||||
} |
||||
|
||||
private static class NoOpCapability implements Capability { |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,171 @@
@@ -0,0 +1,171 @@
|
||||
/* |
||||
* Copyright 2022-2022 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 |
||||
* |
||||
* https://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 org.springframework.cloud.openfeign.aot; |
||||
|
||||
import java.net.URL; |
||||
|
||||
import org.apache.catalina.webresources.TomcatURLStreamHandlerFactory; |
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.junit.jupiter.api.AfterEach; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
||||
import org.springframework.aot.AotDetector; |
||||
import org.springframework.aot.test.generate.TestGenerationContext; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; |
||||
import org.springframework.boot.context.annotation.UserConfigurations; |
||||
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
||||
import org.springframework.boot.test.system.CapturedOutput; |
||||
import org.springframework.boot.test.system.OutputCaptureExtension; |
||||
import org.springframework.boot.test.util.TestPropertyValues; |
||||
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; |
||||
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; |
||||
import org.springframework.cloud.openfeign.EnableFeignClients; |
||||
import org.springframework.cloud.openfeign.FeignAutoConfiguration; |
||||
import org.springframework.cloud.openfeign.FeignClient; |
||||
import org.springframework.context.ApplicationContextInitializer; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.context.aot.ApplicationContextAotGenerator; |
||||
import org.springframework.context.support.GenericApplicationContext; |
||||
import org.springframework.core.test.tools.CompileWithForkedClassLoader; |
||||
import org.springframework.core.test.tools.TestCompiler; |
||||
import org.springframework.javapoet.ClassName; |
||||
import org.springframework.test.util.ReflectionTestUtils; |
||||
import org.springframework.web.bind.annotation.GetMapping; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* AOT processing tests. |
||||
* |
||||
* @author Olga Maciaszek-Sharma |
||||
*/ |
||||
@ExtendWith(OutputCaptureExtension.class) |
||||
public class FeignAotTests { |
||||
|
||||
private static final Log LOG = LogFactory.getLog(FeignAotTests.class); |
||||
|
||||
@BeforeEach |
||||
@AfterEach |
||||
void reset() { |
||||
ReflectionTestUtils.setField(TomcatURLStreamHandlerFactory.class, "instance", null); |
||||
ReflectionTestUtils.setField(URL.class, "factory", null); |
||||
} |
||||
|
||||
@Test |
||||
@CompileWithForkedClassLoader |
||||
@SuppressWarnings("unchecked") |
||||
void shouldStartFeignChildContextsFromAotContributions(CapturedOutput output) { |
||||
WebApplicationContextRunner contextRunner = new WebApplicationContextRunner( |
||||
AnnotationConfigServletWebApplicationContext::new) |
||||
.withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class, |
||||
FeignAutoConfiguration.class)) |
||||
.withConfiguration(UserConfigurations.of(TestFeignConfiguration.class)) |
||||
.withPropertyValues("logging.level.org.springframework.cloud=DEBUG"); |
||||
contextRunner.prepare(context -> { |
||||
TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class); |
||||
ClassName className = new ApplicationContextAotGenerator().processAheadOfTime( |
||||
(GenericApplicationContext) context.getSourceApplicationContext(), generationContext); |
||||
generationContext.writeGeneratedContent(); |
||||
TestCompiler compiler = TestCompiler.forSystem(); |
||||
compiler.with(generationContext).compile(compiled -> { |
||||
ServletWebServerApplicationContext freshApplicationContext = new ServletWebServerApplicationContext(); |
||||
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled |
||||
.getInstance(ApplicationContextInitializer.class, className.toString()); |
||||
initializer.initialize(freshApplicationContext); |
||||
assertThat(output).contains("Creating a FeignClientFactoryBean."); |
||||
assertThat(output).contains("Refreshing FeignClientFactory-test-with-config", |
||||
"Refreshing FeignClientFactory-test"); |
||||
assertThat(output).doesNotContain("Instantiating bean from Test custom config", |
||||
"Instantiating bean from default custom config"); |
||||
TestPropertyValues.of(AotDetector.AOT_ENABLED + "=true") |
||||
.applyToSystemProperties(freshApplicationContext::refresh); |
||||
assertThat(output).contains("Instantiating bean from Test custom config", |
||||
"Instantiating bean from default custom config"); |
||||
assertThat(freshApplicationContext.getBean(TestFeignClient.class)).isNotNull(); |
||||
assertThat(freshApplicationContext.getBean(TestFeignClientWithConfig.class)).isNotNull(); |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
static class TestTarget { |
||||
|
||||
} |
||||
|
||||
@Configuration(proxyBeanMethods = false) |
||||
@EnableFeignClients(clients = { TestFeignClient.class, TestFeignClientWithConfig.class }, |
||||
defaultConfiguration = DefaultConfiguration.class) |
||||
public static class TestFeignConfiguration { |
||||
|
||||
@Autowired |
||||
TestFeignClient testFeignClient; |
||||
|
||||
@Autowired |
||||
TestFeignClientWithConfig testFeignClientWithConfig; |
||||
|
||||
} |
||||
|
||||
public static class TestConfiguration { |
||||
|
||||
@Bean |
||||
TestBean testBean() { |
||||
if (LOG.isDebugEnabled()) { |
||||
LOG.debug("Instantiating bean from Test custom config"); |
||||
} |
||||
return new TestBean(); |
||||
} |
||||
|
||||
} |
||||
|
||||
public static class DefaultConfiguration { |
||||
|
||||
@Bean |
||||
TestBean defaultTestBean() { |
||||
if (LOG.isDebugEnabled()) { |
||||
LOG.debug("Instantiating bean from default custom config"); |
||||
} |
||||
return new TestBean(); |
||||
} |
||||
|
||||
} |
||||
|
||||
public static class TestBean { |
||||
|
||||
} |
||||
|
||||
@FeignClient(value = "test", dismiss404 = true, url = "http://example.com") |
||||
interface TestFeignClient { |
||||
|
||||
@GetMapping |
||||
void test(); |
||||
|
||||
} |
||||
|
||||
@FeignClient(value = "test-with-config", configuration = TestConfiguration.class) |
||||
interface TestFeignClientWithConfig { |
||||
|
||||
@GetMapping |
||||
void test(); |
||||
|
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue