diff --git a/spring-integration-core/src/main/java/org/springframework/integration/aot/GatewayProxyBeanRegistrationAotProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/aot/GatewayProxyBeanRegistrationAotProcessor.java deleted file mode 100644 index bdf4128cc9..0000000000 --- a/spring-integration-core/src/main/java/org/springframework/integration/aot/GatewayProxyBeanRegistrationAotProcessor.java +++ /dev/null @@ -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 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); - } - - } - -} diff --git a/spring-integration-core/src/main/java/org/springframework/integration/aot/GatewayProxyInitializationAotProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/aot/GatewayProxyInitializationAotProcessor.java new file mode 100644 index 0000000000..87958d624c --- /dev/null +++ b/spring-integration-core/src/main/java/org/springframework/integration/aot/GatewayProxyInitializationAotProcessor.java @@ -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> 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))); + }; + } + +} diff --git a/spring-integration-core/src/main/resources/META-INF/spring/aot.factories b/spring-integration-core/src/main/resources/META-INF/spring/aot.factories index 3bc5846a25..e59201aef0 100644 --- a/spring-integration-core/src/main/resources/META-INF/spring/aot.factories +++ b/spring-integration-core/src/main/resources/META-INF/spring/aot.factories @@ -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