Skip to content

Commit

Permalink
Reduce instance supplier use to appease AOT
Browse files Browse the repository at this point in the history
* Remove the use of instance suppliers on bean definitions
that are processed during the AOT phase.

* The remaining areas that use instance suppliers do so
at runtime and do not use reflection, but instead
are passed the configured bean to register.

See spring-cloud/spring-cloud-stream#2655
  • Loading branch information
onobc committed Mar 2, 2023
1 parent dfe45c7 commit bc92c49
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand Down Expand Up @@ -70,6 +70,7 @@
* @author Gary Russell
* @author Michael Wiles
* @author Pierre Lakreb
* @author Chris Bono
*
* @see IntegrationContextUtils
*/
Expand Down Expand Up @@ -153,14 +154,14 @@ public void afterSingletonsInstantiated() {
private void registerBeanFactoryChannelResolver() {
if (!this.beanFactory.containsBeanDefinition(ChannelResolverUtils.CHANNEL_RESOLVER_BEAN_NAME)) {
this.registry.registerBeanDefinition(ChannelResolverUtils.CHANNEL_RESOLVER_BEAN_NAME,
new RootBeanDefinition(BeanFactoryChannelResolver.class, BeanFactoryChannelResolver::new));
new RootBeanDefinition(BeanFactoryChannelResolver.class));
}
}

private void registerMessagePublishingErrorHandler() {
if (!this.beanFactory.containsBeanDefinition(ChannelUtils.MESSAGE_PUBLISHING_ERROR_HANDLER_BEAN_NAME)) {
this.registry.registerBeanDefinition(ChannelUtils.MESSAGE_PUBLISHING_ERROR_HANDLER_BEAN_NAME,
new RootBeanDefinition(MessagePublishingErrorHandler.class, MessagePublishingErrorHandler::new));
new RootBeanDefinition(MessagePublishingErrorHandler.class));
}
}

Expand All @@ -173,8 +174,7 @@ private void registerNullChannel() {
BeanDefinition nullChannelDefinition = null;
BeanFactory beanFactoryToUse = this.beanFactory;
do {
if (beanFactoryToUse instanceof ConfigurableListableBeanFactory) {
ConfigurableListableBeanFactory listable = (ConfigurableListableBeanFactory) beanFactoryToUse;
if (beanFactoryToUse instanceof ConfigurableListableBeanFactory listable) {
if (listable.containsBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME)) {
nullChannelDefinition =
listable.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
Expand All @@ -193,7 +193,7 @@ private void registerNullChannel() {
}
else {
this.registry.registerBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME,
new RootBeanDefinition(NullChannel.class, NullChannel::new));
new RootBeanDefinition(NullChannel.class));
}
}

Expand All @@ -208,9 +208,8 @@ private void registerErrorChannel() {
LOGGER.info(() -> "No bean named '" + IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME +
"' has been explicitly defined. " +
"Therefore, a default PublishSubscribeChannel will be created.");

this.registry.registerBeanDefinition(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME,
BeanDefinitionBuilder.rootBeanDefinition(PublishSubscribeChannel.class, this::createErrorChannel)
BeanDefinitionBuilder.rootBeanDefinition(PublishSubscribeChannel.class)
.addConstructorArgValue(IntegrationProperties.getExpressionFor(
IntegrationProperties.ERROR_CHANNEL_REQUIRE_SUBSCRIBERS))
.addPropertyValue("ignoreFailures", IntegrationProperties.getExpressionFor(
Expand All @@ -220,15 +219,13 @@ private void registerErrorChannel() {
String errorLoggerBeanName =
IntegrationContextUtils.ERROR_LOGGER_BEAN_NAME + IntegrationConfigUtils.HANDLER_ALIAS_SUFFIX;
this.registry.registerBeanDefinition(errorLoggerBeanName,
BeanDefinitionBuilder.genericBeanDefinition(LoggingHandler.class,
() -> new LoggingHandler(LoggingHandler.Level.ERROR))
BeanDefinitionBuilder.genericBeanDefinition(LoggingHandler.class)
.addConstructorArgValue(LoggingHandler.Level.ERROR)
.addPropertyValue(IntegrationNamespaceUtils.ORDER, Ordered.LOWEST_PRECEDENCE - 100)
.getBeanDefinition());

BeanDefinitionBuilder loggingEndpointBuilder =
BeanDefinitionBuilder.genericBeanDefinition(ConsumerEndpointFactoryBean.class,
ConsumerEndpointFactoryBean::new)
BeanDefinitionBuilder.genericBeanDefinition(ConsumerEndpointFactoryBean.class)
.addPropertyValue("inputChannelName", IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)
.addPropertyReference("handler", errorLoggerBeanName);

Expand All @@ -239,20 +236,14 @@ private void registerErrorChannel() {
}
}

private PublishSubscribeChannel createErrorChannel() {
IntegrationProperties integrationProperties = IntegrationContextUtils.getIntegrationProperties(this.beanFactory);
return new PublishSubscribeChannel(integrationProperties.isErrorChannelRequireSubscribers());
}

/**
* Register {@link IntegrationEvaluationContextFactoryBean}
* and {@link IntegrationSimpleEvaluationContextFactoryBean} beans, if necessary.
*/
private void registerIntegrationEvaluationContext() {
if (!this.registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME)) {
BeanDefinitionBuilder integrationEvaluationContextBuilder =
BeanDefinitionBuilder.genericBeanDefinition(IntegrationEvaluationContextFactoryBean.class,
IntegrationEvaluationContextFactoryBean::new)
BeanDefinitionBuilder.genericBeanDefinition(IntegrationEvaluationContextFactoryBean.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

this.registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME,
Expand All @@ -263,8 +254,7 @@ private void registerIntegrationEvaluationContext() {
IntegrationContextUtils.INTEGRATION_SIMPLE_EVALUATION_CONTEXT_BEAN_NAME)) {

BeanDefinitionBuilder integrationEvaluationContextBuilder =
BeanDefinitionBuilder.genericBeanDefinition(IntegrationSimpleEvaluationContextFactoryBean.class,
IntegrationSimpleEvaluationContextFactoryBean::new)
BeanDefinitionBuilder.genericBeanDefinition(IntegrationSimpleEvaluationContextFactoryBean.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

this.registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_SIMPLE_EVALUATION_CONTEXT_BEAN_NAME,
Expand All @@ -286,7 +276,7 @@ private void registerIdGeneratorConfigurer() {
return;
}
}
RootBeanDefinition beanDefinition = new RootBeanDefinition(clazz, IdGeneratorConfigurer::new);
RootBeanDefinition beanDefinition = new RootBeanDefinition(clazz);
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, this.registry);
}
Expand All @@ -300,8 +290,7 @@ private void registerTaskScheduler() {
"' has been explicitly defined. " +
"Therefore, a default ThreadPoolTaskScheduler will be created.");
BeanDefinition scheduler =
BeanDefinitionBuilder.genericBeanDefinition(ThreadPoolTaskScheduler.class,
ThreadPoolTaskScheduler::new)
BeanDefinitionBuilder.genericBeanDefinition(ThreadPoolTaskScheduler.class)
.addPropertyValue("poolSize", IntegrationProperties.getExpressionFor(
IntegrationProperties.TASK_SCHEDULER_POOL_SIZE))
.addPropertyValue("threadNamePrefix", "task-scheduler-")
Expand All @@ -319,7 +308,7 @@ private void registerTaskScheduler() {
private void registerIntegrationProperties() {
if (!this.beanFactory.containsBean(IntegrationContextUtils.INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME)) {
BeanDefinition userProperties =
BeanDefinitionBuilder.genericBeanDefinition(PropertiesFactoryBean.class, PropertiesFactoryBean::new)
BeanDefinitionBuilder.genericBeanDefinition(PropertiesFactoryBean.class)
.addPropertyValue("locations", "classpath*:META-INF/spring.integration.properties")
.getBeanDefinition();

Expand Down Expand Up @@ -374,7 +363,7 @@ private void xpath(int registryId) throws LinkageError {
private void registerRoleController() {
if (!this.beanFactory.containsBean(IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER)) {
this.registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_LIFECYCLE_ROLE_CONTROLLER,
new RootBeanDefinition(SmartLifecycleRoleController.class, SmartLifecycleRoleController::new));
new RootBeanDefinition(SmartLifecycleRoleController.class));
}
}

Expand All @@ -384,7 +373,7 @@ private void registerRoleController() {
private void registerMessageBuilderFactory() {
if (!this.beanFactory.containsBean(IntegrationUtils.INTEGRATION_MESSAGE_BUILDER_FACTORY_BEAN_NAME)) {
BeanDefinitionBuilder mbfBuilder = BeanDefinitionBuilder
.genericBeanDefinition(DefaultMessageBuilderFactory.class, DefaultMessageBuilderFactory::new)
.genericBeanDefinition(DefaultMessageBuilderFactory.class)
.addPropertyValue("readOnlyHeaders",
IntegrationProperties.getExpressionFor(IntegrationProperties.READ_ONLY_HEADERS));
this.registry.registerBeanDefinition(
Expand All @@ -401,7 +390,7 @@ private void registerHeaderChannelRegistry() {
"' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.");

this.registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME,
new RootBeanDefinition(DefaultHeaderChannelRegistry.class, DefaultHeaderChannelRegistry::new));
new RootBeanDefinition(DefaultHeaderChannelRegistry.class));
}
}

Expand All @@ -412,8 +401,7 @@ private void registerGlobalChannelInterceptorProcessor() {
if (!this.registry.containsBeanDefinition(
IntegrationContextUtils.GLOBAL_CHANNEL_INTERCEPTOR_PROCESSOR_BEAN_NAME)) {
BeanDefinitionBuilder builder =
BeanDefinitionBuilder.genericBeanDefinition(GlobalChannelInterceptorProcessor.class,
GlobalChannelInterceptorProcessor::new)
BeanDefinitionBuilder.genericBeanDefinition(GlobalChannelInterceptorProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

this.registry.registerBeanDefinition(IntegrationContextUtils.GLOBAL_CHANNEL_INTERCEPTOR_PROCESSOR_BEAN_NAME,
Expand All @@ -430,8 +418,7 @@ private void registerDefaultDatatypeChannelMessageConverter() {

this.registry.registerBeanDefinition(
IntegrationContextUtils.INTEGRATION_DATATYPE_CHANNEL_MESSAGE_CONVERTER_BEAN_NAME,
new RootBeanDefinition(DefaultDatatypeChannelMessageConverter.class,
DefaultDatatypeChannelMessageConverter::new));
new RootBeanDefinition(DefaultDatatypeChannelMessageConverter.class));
}
}

Expand All @@ -442,8 +429,7 @@ private void registerDefaultDatatypeChannelMessageConverter() {
private void registerArgumentResolverMessageConverter() {
if (!this.beanFactory.containsBean(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME)) {
this.registry.registerBeanDefinition(IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME,
new RootBeanDefinition(ConfigurableCompositeMessageConverter.class,
ConfigurableCompositeMessageConverter::new));
new RootBeanDefinition(ConfigurableCompositeMessageConverter.class));
}
}

Expand All @@ -466,8 +452,7 @@ private void registerListMessageHandlerMethodFactory() {
}

private static BeanDefinitionBuilder createMessageHandlerMethodFactoryBeanDefinition(boolean listCapable) {
return BeanDefinitionBuilder.genericBeanDefinition(IntegrationMessageHandlerMethodFactory.class,
() -> new IntegrationMessageHandlerMethodFactory(listCapable))
return BeanDefinitionBuilder.genericBeanDefinition(IntegrationMessageHandlerMethodFactory.class)
.addConstructorArgValue(listCapable)
.addPropertyReference("messageConverter",
IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 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.
Expand Down Expand Up @@ -45,6 +45,7 @@
* to Consumer Endpoints are present.
*
* @author Artem Bilan
* @author Chris Bono
*
* @since 4.1
*/
Expand Down Expand Up @@ -83,8 +84,7 @@ else if (beanDefinition instanceof AnnotatedBeanDefinition) {

if (!idempotentEndpointsMapping.isEmpty()) {
BeanDefinition bd =
BeanDefinitionBuilder.rootBeanDefinition(IdempotentReceiverAutoProxyCreator.class,
IdempotentReceiverAutoProxyCreator::new)
BeanDefinitionBuilder.rootBeanDefinition(IdempotentReceiverAutoProxyCreator.class)
.addPropertyValue("idempotentEndpointsMapping", idempotentEndpointsMapping)
.getBeanDefinition();
registry.registerBeanDefinition(IDEMPOTENT_RECEIVER_AUTO_PROXY_CREATOR_BEAN_NAME, bd);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 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.
Expand Down Expand Up @@ -65,6 +65,7 @@
*
* @author Artem Bilan
* @author Gary Russell
* @author Chris Bono
*
* @since 4.0
*/
Expand Down Expand Up @@ -109,7 +110,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
}

registry.registerBeanDefinition(BEAN_NAME,
BeanDefinitionBuilder.genericBeanDefinition(IntegrationComponentScanRegistrar.class, () -> this)
BeanDefinitionBuilder.genericBeanDefinition(IntegrationComponentScanRegistrar.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
.getBeanDefinition());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 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.
Expand Down Expand Up @@ -29,6 +29,7 @@
* Shared utility methods for Integration configuration.
*
* @author Artem Bilan
* @author Chris Bono
*
* @since 4.0
*/
Expand Down Expand Up @@ -57,15 +58,14 @@ public static void registerSpelFunctionBean(BeanDefinitionRegistry registry, Str
String methodSignature) {

BeanDefinitionBuilder builder =
BeanDefinitionBuilder.genericBeanDefinition(SpelFunctionFactoryBean.class,
() -> new SpelFunctionFactoryBean(aClass, methodSignature))
BeanDefinitionBuilder.genericBeanDefinition(SpelFunctionFactoryBean.class)
.addConstructorArgValue(aClass)
.addConstructorArgValue(methodSignature);
registry.registerBeanDefinition(functionId, builder.getBeanDefinition());
}

public static void autoCreateDirectChannel(String channelName, BeanDefinitionRegistry registry) {
registry.registerBeanDefinition(channelName, new RootBeanDefinition(DirectChannel.class, DirectChannel::new));
registry.registerBeanDefinition(channelName, new RootBeanDefinition(DirectChannel.class));
}

public static BeanNameGenerator annotationBeanNameGenerator(BeanDefinitionRegistry registry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 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.
Expand Down Expand Up @@ -38,6 +38,7 @@
*
* @author Artem Bilan
* @author Gary Russell
* @author Chris Bono
*
* @since 4.0
*/
Expand All @@ -60,13 +61,12 @@ public void initialize(ConfigurableListableBeanFactory beanFactory) throws Beans

if (!registry.containsBeanDefinition(IntegrationContextUtils.CONVERTER_REGISTRAR_BEAN_NAME)) {
registry.registerBeanDefinition(IntegrationContextUtils.CONVERTER_REGISTRAR_BEAN_NAME,
new RootBeanDefinition(ConverterRegistrar.class, ConverterRegistrar::new));
new RootBeanDefinition(ConverterRegistrar.class));
}

if (!registry.containsBeanDefinition(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME)) {
registry.registerBeanDefinition(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME,
new RootBeanDefinition(CustomConversionServiceFactoryBean.class,
CustomConversionServiceFactoryBean::new));
new RootBeanDefinition(CustomConversionServiceFactoryBean.class));
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 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.
Expand Down Expand Up @@ -31,6 +31,7 @@
*
* @author Artem Bilan
* @author Gary Russell
* @author Chris Bono
*
* @since 4.0
*/
Expand Down Expand Up @@ -73,8 +74,7 @@ public void registerBeanDefinitions(@Nullable AnnotationMetadata importingClassM
private void registerDefaultConfiguringBeanFactoryPostProcessor(BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(IntegrationContextUtils.DEFAULT_CONFIGURING_POSTPROCESSOR_BEAN_NAME)) {
BeanDefinitionBuilder postProcessorBuilder =
BeanDefinitionBuilder.genericBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class,
DefaultConfiguringBeanFactoryPostProcessor::new)
BeanDefinitionBuilder.genericBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(IntegrationContextUtils.DEFAULT_CONFIGURING_POSTPROCESSOR_BEAN_NAME,
postProcessorBuilder.getBeanDefinition());
Expand All @@ -90,8 +90,7 @@ private void registerIntegrationConfigurationBeanFactoryPostProcessor(BeanDefini
IntegrationContextUtils.INTEGRATION_CONFIGURATION_POST_PROCESSOR_BEAN_NAME)) {

BeanDefinitionBuilder postProcessorBuilder =
BeanDefinitionBuilder.genericBeanDefinition(IntegrationConfigurationBeanFactoryPostProcessor.class,
IntegrationConfigurationBeanFactoryPostProcessor::new)
BeanDefinitionBuilder.genericBeanDefinition(IntegrationConfigurationBeanFactoryPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_CONFIGURATION_POST_PROCESSOR_BEAN_NAME,
postProcessorBuilder.getBeanDefinition());
Expand All @@ -108,8 +107,7 @@ private void registerIntegrationConfigurationBeanFactoryPostProcessor(BeanDefini
private void registerMessagingAnnotationPostProcessors(BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(IntegrationContextUtils.MESSAGING_ANNOTATION_POSTPROCESSOR_NAME)) {
BeanDefinitionBuilder builder =
BeanDefinitionBuilder.genericBeanDefinition(MessagingAnnotationPostProcessor.class,
MessagingAnnotationPostProcessor::new)
BeanDefinitionBuilder.genericBeanDefinition(MessagingAnnotationPostProcessor.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);

registry.registerBeanDefinition(IntegrationContextUtils.MESSAGING_ANNOTATION_POSTPROCESSOR_NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2023 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.
Expand Down Expand Up @@ -35,6 +35,7 @@
*
* @author Artem Bilan
* @author Gary Russell
* @author Chris Bono
*
* @since 4.0
*/
Expand All @@ -61,8 +62,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
}

BeanDefinition messageHistoryConfigurer =
BeanDefinitionBuilder.genericBeanDefinition(MessageHistoryConfigurer.class,
MessageHistoryConfigurer::new)
BeanDefinitionBuilder.genericBeanDefinition(MessageHistoryConfigurer.class)
.addPropertyValue("componentNamePatterns", patterns)
.getBeanDefinition();

Expand Down
Loading

0 comments on commit bc92c49

Please sign in to comment.