GH-3903: Improve AOT for gateway proxy beans (#3904)
* GH-3903: Improve AOT for gateway proxy beans Fixes https://github.com/spring-projects/spring-integration/issues/3903 * Fix `MessagingGatewayRegistrar` to set a `targetType` on the bean definition instead of `FactoryBean.OBJECT_TYPE_ATTRIBUTE` * Rework `GatewayProxyBeanRegistrationAotProcessor` to the `GatewayProxyInitializationAotProcessor implements BeanFactoryInitializationAotProcessor` to avoid custom code generation * Remove tests which rely on the `FactoryBean.OBJECT_TYPE_ATTRIBUTE` * * Add `setBeanClass(GatewayProxyFactoryBean.class)` to satisfy Spring container expectations * * Rework `GatewayProxyInitializationAotProcessor` to deal with generics we are exposing now on the `ProxyFactoryBean`
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* Copyright 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.integration.aot;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.aop.SpringProxy;
|
||||
import org.springframework.aop.framework.Advised;
|
||||
import org.springframework.aot.generate.GenerationContext;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
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.aot.BeanRegistrationCodeFragments;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragmentsDecorator;
|
||||
import org.springframework.beans.factory.support.RegisteredBean;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.core.DecoratingProxy;
|
||||
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
|
||||
import org.springframework.javapoet.CodeBlock;
|
||||
|
||||
/**
|
||||
* {@link BeanRegistrationAotProcessor} for registering proxy interfaces of the {@link GatewayProxyFactoryBean} beans.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 6.0
|
||||
*/
|
||||
class GatewayProxyBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
|
||||
|
||||
@Override
|
||||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
||||
if (GatewayProxyFactoryBean.class.isAssignableFrom(registeredBean.getBeanClass())) {
|
||||
return BeanRegistrationAotContribution
|
||||
.withCustomCodeFragments(GatewayProxyBeanRegistrationCodeFragments::new);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class GatewayProxyBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
|
||||
|
||||
GatewayProxyBeanRegistrationCodeFragments(BeanRegistrationCodeFragments delegate) {
|
||||
super(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CodeBlock generateSetBeanDefinitionPropertiesCode(GenerationContext generationContext,
|
||||
BeanRegistrationCode beanRegistrationCode, RootBeanDefinition beanDefinition,
|
||||
Predicate<String> attributeFilter) {
|
||||
|
||||
Class<?> serviceInterface = (Class<?>) beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
|
||||
generationContext.getRuntimeHints().proxies()
|
||||
.registerJdkProxy(serviceInterface, SpringProxy.class, Advised.class, DecoratingProxy.class);
|
||||
|
||||
return super.generateSetBeanDefinitionPropertiesCode(generationContext, beanRegistrationCode,
|
||||
beanDefinition, FactoryBean.OBJECT_TYPE_ATTRIBUTE::equals);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 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.integration.aot;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.aop.framework.AopProxyUtils;
|
||||
import org.springframework.aop.framework.ProxyFactoryBean;
|
||||
import org.springframework.aot.hint.ProxyHints;
|
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
|
||||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RegisteredBean;
|
||||
import org.springframework.integration.gateway.GatewayProxyFactoryBean;
|
||||
import org.springframework.integration.gateway.RequestReplyExchanger;
|
||||
|
||||
/**
|
||||
* A {@link BeanFactoryInitializationAotProcessor} for registering proxy interfaces
|
||||
* of the {@link GatewayProxyFactoryBean} beans.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 6.0
|
||||
*/
|
||||
class GatewayProxyInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {
|
||||
|
||||
@Override
|
||||
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
|
||||
List<? extends Class<?>> gatewayProxyInterfaces =
|
||||
Arrays.stream(beanFactory.getBeanDefinitionNames())
|
||||
.map((beanName) -> RegisteredBean.of(beanFactory, beanName))
|
||||
.filter((bean) -> ProxyFactoryBean.class.isAssignableFrom(bean.getBeanClass()))
|
||||
.map((bean) -> bean.getBeanType().getGeneric(0).resolve(RequestReplyExchanger.class))
|
||||
.toList();
|
||||
|
||||
return (generationContext, beanFactoryInitializationCode) -> {
|
||||
ProxyHints proxyHints = generationContext.getRuntimeHints().proxies();
|
||||
gatewayProxyInterfaces.forEach((gatewayInterface) ->
|
||||
proxyHints.registerJdkProxy(AopProxyUtils.completeJdkProxyInterfaces(gatewayInterface)));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,3 +1,3 @@
|
||||
org.springframework.aot.hint.RuntimeHintsRegistrar=org.springframework.integration.aot.CoreRuntimeHints
|
||||
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=org.springframework.integration.aot.GatewayProxyBeanRegistrationAotProcessor
|
||||
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=org.springframework.integration.aot.GatewayProxyInitializationAotProcessor
|
||||
org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter=org.springframework.integration.aot.IntegrationBeanRegistrationExcludeFilter
|
||||
|
||||
Reference in New Issue
Block a user