Browse Source

Add native image support.

aot-support
Olga Maciaszek-Sharma 2 years ago
parent
commit
c44782d8b4
  1. 19
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java
  2. 2
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientsRegistrar.java
  3. 11
      spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/aot/FeignClientBeanFactoryInitializationAotProcessor.java
  4. 2
      spring-cloud-openfeign-core/src/main/resources/META-INF/spring/aot.factories
  5. 1
      spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/aot/FeignAotTests.java

19
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java

@ -44,6 +44,10 @@ import okhttp3.ConnectionPool;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeReference;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@ -73,6 +77,7 @@ import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2A
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService; import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.util.ClassUtils;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
@ -373,3 +378,17 @@ public class FeignAutoConfiguration {
} }
} }
class FeignHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
if (!ClassUtils.isPresent("feign.Feign", classLoader)) {
return;
}
hints.reflection().registerType(TypeReference.of(FeignClientFactoryBean.class),
hint -> hint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.DECLARED_FIELDS));
}
}

2
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignClientsRegistrar.java

@ -71,7 +71,7 @@ import org.springframework.util.StringUtils;
class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware { class FeignClientsRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware, EnvironmentAware {
// patterned after Spring Integration IntegrationComponentScanRegistrar // patterned after Spring Integration IntegrationComponentScanRegistrar
// and RibbonClientsConfigurationRegistgrar // and RibbonClientsConfigurationRegistrar
private ResourceLoader resourceLoader; private ResourceLoader resourceLoader;

11
spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/aot/FeignClientBeanFactoryInitializationAotProcessor.java

@ -25,6 +25,7 @@ import javax.lang.model.element.Modifier;
import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodReference; import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.hint.ProxyHints;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
@ -100,16 +101,20 @@ public class FeignClientBeanFactoryInitializationAotProcessor
@Override @Override
public void applyTo(GenerationContext generationContext, public void applyTo(GenerationContext generationContext,
BeanFactoryInitializationCode beanFactoryInitializationCode) { BeanFactoryInitializationCode beanFactoryInitializationCode) {
ProxyHints proxyHints = generationContext.getRuntimeHints().proxies();
Set<String> feignClientRegistrationMethods = feignClientBeanDefinitions.values().stream() Set<String> feignClientRegistrationMethods = feignClientBeanDefinitions.values().stream()
.map(beanDefinition -> { .map(beanDefinition -> {
Assert.notNull(beanDefinition, "beanDefinition cannot be null"); Assert.notNull(beanDefinition, "beanDefinition cannot be null");
Assert.isInstanceOf(GenericBeanDefinition.class, beanDefinition); Assert.isInstanceOf(GenericBeanDefinition.class, beanDefinition);
GenericBeanDefinition registeredBeanDefinition = (GenericBeanDefinition) beanDefinition; GenericBeanDefinition registeredBeanDefinition = (GenericBeanDefinition) beanDefinition;
MutablePropertyValues feignClientProperties = registeredBeanDefinition.getPropertyValues(); 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() return beanFactoryInitializationCode.getMethods()
.add(buildMethodName((String) feignClientProperties.get("type")), .add(buildMethodName(className), method -> generateFeignClientRegistrationMethod(method,
method -> generateFeignClientRegistrationMethod(method, feignClientProperties, feignClientProperties, registeredBeanDefinition))
registeredBeanDefinition))
.getName(); .getName();
}).collect(Collectors.toSet()); }).collect(Collectors.toSet());
MethodReference initializerMethod = beanFactoryInitializationCode.getMethods() MethodReference initializerMethod = beanFactoryInitializationCode.getMethods()

2
spring-cloud-openfeign-core/src/main/resources/META-INF/spring/aot.factories

@ -0,0 +1,2 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.cloud.openfeign.FeignHints

1
spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/aot/FeignAotTests.java

@ -160,7 +160,6 @@ public class FeignAotTests {
} }
// TODO: verify other annotation parameters
@FeignClient(value = "test-with-config", configuration = TestConfiguration.class) @FeignClient(value = "test-with-config", configuration = TestConfiguration.class)
interface TestFeignClientWithConfig { interface TestFeignClientWithConfig {

Loading…
Cancel
Save