Skip to content

Commit 0549dbd

Browse files
authored
GH-3502: Make the framework compatible with Native (#3531)
Fixes #3502
1 parent 5a42eff commit 0549dbd

File tree

12 files changed

+266
-277
lines changed

12 files changed

+266
-277
lines changed

spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java

Lines changed: 170 additions & 140 deletions
Large diffs are not rendered by default.

spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationManagementConfigurer.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package org.springframework.integration.config;
1818

1919
import java.util.HashSet;
20-
import java.util.Map;
21-
import java.util.Map.Entry;
2220
import java.util.Set;
2321

2422
import org.springframework.beans.BeansException;
@@ -130,39 +128,18 @@ public void afterSingletonsInstantiated() {
130128
Assert.state(this.applicationContext != null, "'applicationContext' must not be null");
131129
Assert.state(MANAGEMENT_CONFIGURER_NAME.equals(this.beanName), getClass().getSimpleName()
132130
+ " bean name must be " + MANAGEMENT_CONFIGURER_NAME);
131+
133132
if (obtainMetricsCaptor() != null) {
134-
injectCaptor();
135133
registerComponentGauges();
136134
}
137-
Map<String, IntegrationManagement> managed = this.applicationContext
138-
.getBeansOfType(IntegrationManagement.class);
139-
for (Entry<String, IntegrationManagement> entry : managed.entrySet()) {
140-
IntegrationManagement bean = entry.getValue();
141-
if (!getOverrides(bean).loggingConfigured) {
142-
bean.setLoggingEnabled(this.defaultLoggingEnabled);
143-
}
144-
}
145-
this.singletonsInstantiated = true;
146-
}
147135

148-
private void injectCaptor() {
149-
Map<String, IntegrationManagement> managed =
150-
this.applicationContext.getBeansOfType(IntegrationManagement.class);
151-
for (Entry<String, IntegrationManagement> entry : managed.entrySet()) {
152-
IntegrationManagement bean = entry.getValue();
153-
if (!getOverrides(bean).loggingConfigured) {
154-
bean.setLoggingEnabled(this.defaultLoggingEnabled);
155-
}
156-
bean.registerMetricsCaptor(this.metricsCaptor);
157-
}
158-
}
136+
for (IntegrationManagement integrationManagement :
137+
this.applicationContext.getBeansOfType(IntegrationManagement.class).values()) {
159138

160-
@Override
161-
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
162-
if (this.singletonsInstantiated && obtainMetricsCaptor() != null && bean instanceof IntegrationManagement) {
163-
((IntegrationManagement) bean).registerMetricsCaptor(this.metricsCaptor);
139+
enhanceIntegrationManagement(integrationManagement);
164140
}
165-
return bean;
141+
142+
this.singletonsInstantiated = true;
166143
}
167144

168145
private void registerComponentGauges() {
@@ -185,6 +162,23 @@ private void registerComponentGauges() {
185162
.build());
186163
}
187164

165+
private void enhanceIntegrationManagement(IntegrationManagement integrationManagement) {
166+
if (!getOverrides(integrationManagement).loggingConfigured) {
167+
integrationManagement.setLoggingEnabled(this.defaultLoggingEnabled);
168+
}
169+
if (this.metricsCaptor != null) {
170+
integrationManagement.registerMetricsCaptor(this.metricsCaptor);
171+
}
172+
}
173+
174+
@Override
175+
public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
176+
if (this.singletonsInstantiated && bean instanceof IntegrationManagement) {
177+
enhanceIntegrationManagement((IntegrationManagement) bean);
178+
}
179+
return bean;
180+
}
181+
188182
@Override public void onApplicationEvent(ContextClosedEvent event) {
189183
if (event.getApplicationContext().equals(this.applicationContext)) {
190184
this.gauges.forEach(MeterFacade::remove);

spring-integration-core/src/main/java/org/springframework/integration/config/IntegrationRegistrar.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.support.ManagedSet;
2525
import org.springframework.context.ApplicationContextException;
2626
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
27+
import org.springframework.core.NativeDetector;
2728
import org.springframework.core.type.AnnotationMetadata;
2829
import org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor;
2930
import org.springframework.integration.context.IntegrationContextUtils;
@@ -46,7 +47,7 @@ public class IntegrationRegistrar implements ImportBeanDefinitionRegistrar {
4647
IntegrationRegistrar.class.getClassLoader())) {
4748

4849
throw new ApplicationContextException("Starting with Spring Integration 5.0, "
49-
+ "the 'spring-integration-java-dsl' dependency is no longer needed; "
50+
+ "the 'spring-integration-java-dsl' dependency is no longer needed; "
5051
+ "the Java DSL has been merged into the core project. "
5152
+ "If it is present on the classpath, it will cause class loading conflicts.");
5253
}
@@ -108,7 +109,8 @@ private void registerImplicitChannelCreator(BeanDefinitionRegistry registry) {
108109
private void registerDefaultConfiguringBeanFactoryPostProcessor(BeanDefinitionRegistry registry) {
109110
if (!registry.containsBeanDefinition(IntegrationContextUtils.DEFAULT_CONFIGURING_POSTPROCESSOR_BEAN_NAME)) {
110111
BeanDefinitionBuilder postProcessorBuilder =
111-
BeanDefinitionBuilder.genericBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class);
112+
BeanDefinitionBuilder.genericBeanDefinition(DefaultConfiguringBeanFactoryPostProcessor.class,
113+
DefaultConfiguringBeanFactoryPostProcessor::new);
112114
BeanDefinitionHolder postProcessorHolder = new BeanDefinitionHolder(
113115
postProcessorBuilder.getBeanDefinition(),
114116
IntegrationContextUtils.DEFAULT_CONFIGURING_POSTPROCESSOR_BEAN_NAME);
@@ -121,7 +123,8 @@ private void registerDefaultConfiguringBeanFactoryPostProcessor(BeanDefinitionRe
121123
* to process the external Integration infrastructure.
122124
*/
123125
private void registerIntegrationConfigurationBeanFactoryPostProcessor(BeanDefinitionRegistry registry) {
124-
if (!registry.containsBeanDefinition(
126+
if (!(NativeDetector.inNativeImage()) // Spring Native detects all the 'spring.factories'
127+
&& !registry.containsBeanDefinition(
125128
IntegrationContextUtils.INTEGRATION_CONFIGURATION_POST_PROCESSOR_BEAN_NAME)) {
126129

127130
BeanDefinitionBuilder postProcessorBuilder = BeanDefinitionBuilder
@@ -143,7 +146,8 @@ private void registerIntegrationConfigurationBeanFactoryPostProcessor(BeanDefini
143146
private void registerMessagingAnnotationPostProcessors(AnnotationMetadata meta, BeanDefinitionRegistry registry) {
144147
if (!registry.containsBeanDefinition(IntegrationContextUtils.MESSAGING_ANNOTATION_POSTPROCESSOR_NAME)) {
145148
BeanDefinitionBuilder builder =
146-
BeanDefinitionBuilder.genericBeanDefinition(MessagingAnnotationPostProcessor.class)
149+
BeanDefinitionBuilder.genericBeanDefinition(MessagingAnnotationPostProcessor.class,
150+
MessagingAnnotationPostProcessor::new)
147151
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
148152

149153
registry.registerBeanDefinition(IntegrationContextUtils.MESSAGING_ANNOTATION_POSTPROCESSOR_NAME,

spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationContextUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public abstract class IntegrationContextUtils {
8585
IntegrationConfigUtils.BASE_PACKAGE + ".internalPublisherAnnotationBeanPostProcessor";
8686

8787
public static final String INTEGRATION_CONFIGURATION_POST_PROCESSOR_BEAN_NAME =
88-
"IntegrationConfigurationBeanFactoryPostProcessor";
88+
"integrationConfigurationBeanFactoryPostProcessor";
8989

9090
public static final String INTEGRATION_MESSAGE_HISTORY_CONFIGURER_BEAN_NAME = "messageHistoryConfigurer";
9191

spring-integration-core/src/main/java/org/springframework/integration/handler/support/PayloadExpressionArgumentResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@
3636
*
3737
* @since 5.0
3838
*
39-
* @see org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver
39+
* @see org.springframework.messaging.handler.annotation.support.PayloadMethodArgumentResolver
4040
*/
4141
public class PayloadExpressionArgumentResolver extends AbstractExpressionEvaluator
4242
implements HandlerMethodArgumentResolver {

spring-integration-core/src/main/java/org/springframework/integration/support/DefaultMessageBuilderFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.util.Arrays;
2020

21+
import org.springframework.lang.Nullable;
2122
import org.springframework.messaging.Message;
2223

2324
/**
@@ -39,7 +40,7 @@ public class DefaultMessageBuilderFactory implements MessageBuilderFactory {
3940
* and {@link org.springframework.messaging.MessageHeaders#TIMESTAMP}.
4041
* @since 4.3.2
4142
*/
42-
public void setReadOnlyHeaders(String... readOnlyHeaders) {
43+
public void setReadOnlyHeaders(@Nullable String... readOnlyHeaders) {
4344
this.readOnlyHeaders = readOnlyHeaders != null ? Arrays.copyOf(readOnlyHeaders, readOnlyHeaders.length) : null;
4445
}
4546

spring-integration-core/src/main/java/org/springframework/integration/support/SmartLifecycleRoleController.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2020 the original author or authors.
2+
* Copyright 2015-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,6 +69,14 @@ public class SmartLifecycleRoleController implements ApplicationListener<Abstrac
6969

7070
private ApplicationContext applicationContext;
7171

72+
/**
73+
* Construct an instance without any lifecycle initially: can be added later on via
74+
* {@link #addLifecycleToRole(String, SmartLifecycle)}.
75+
* @since 5.5
76+
*/
77+
public SmartLifecycleRoleController() {
78+
}
79+
7280
/**
7381
* Construct an instance with the provided lists of roles and lifecycles, which must be of equal length.
7482
* @param roles the roles.

spring-integration-core/src/main/java/org/springframework/integration/support/management/micrometer/MicrometerMetricsCaptorRegistrar.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.springframework.core.type.AnnotationMetadata;
2525
import org.springframework.util.ClassUtils;
2626

27+
import io.micrometer.core.instrument.MeterRegistry;
28+
2729
/**
2830
* An {@link ImportBeanDefinitionRegistrar} to conditionally add a {@link MicrometerMetricsCaptor}
2931
* bean when {@code io.micrometer.core.instrument.MeterRegistry} is present in classpath and
@@ -35,30 +37,19 @@
3537
*/
3638
public class MicrometerMetricsCaptorRegistrar implements ImportBeanDefinitionRegistrar {
3739

38-
private static final Class<?> METER_REGISTRY_CLASS;
39-
40-
static {
41-
Class<?> aClass = null;
42-
try {
43-
aClass = ClassUtils.forName("io.micrometer.core.instrument.MeterRegistry",
44-
ClassUtils.getDefaultClassLoader());
45-
}
46-
catch (ClassNotFoundException e) {
47-
// Ignore - no Micrometer in classpath
48-
}
49-
METER_REGISTRY_CLASS = aClass;
50-
}
40+
private static final boolean METER_REGISTRY_PRESENT
41+
= ClassUtils.isPresent("io.micrometer.core.instrument.MeterRegistry", ClassUtils.getDefaultClassLoader());
5142

5243
@Override
5344
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
5445
ListableBeanFactory beanFactory = (ListableBeanFactory) registry;
55-
if (METER_REGISTRY_CLASS != null
46+
if (METER_REGISTRY_PRESENT
5647
&& !registry.containsBeanDefinition(MicrometerMetricsCaptor.MICROMETER_CAPTOR_NAME)
57-
&& beanFactory.getBeanNamesForType(METER_REGISTRY_CLASS, false, false).length > 0) {
48+
&& beanFactory.getBeanNamesForType(MeterRegistry.class, false, false).length > 0) {
5849

5950
registry.registerBeanDefinition(MicrometerMetricsCaptor.MICROMETER_CAPTOR_NAME,
60-
BeanDefinitionBuilder.genericBeanDefinition(MicrometerMetricsCaptor.class)
61-
.addConstructorArgValue(beanFactory.getBeanProvider(METER_REGISTRY_CLASS))
51+
BeanDefinitionBuilder.genericBeanDefinition(MicrometerMetricsCaptor.class,
52+
() -> new MicrometerMetricsCaptor(beanFactory.getBeanProvider(MeterRegistry.class)))
6253
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
6354
.getBeanDefinition());
6455
}

spring-integration-http/src/main/java/org/springframework/integration/http/config/EnableIntegrationGraphController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@
4040
@Target(ElementType.TYPE)
4141
@Retention(RetentionPolicy.RUNTIME)
4242
@Inherited
43-
@Import(IntegrationGraphControllerRegistrarImportSelector.class)
43+
@Import(IntegrationGraphControllerRegistrar.class)
4444
public @interface EnableIntegrationGraphController {
4545

4646
/**

spring-integration-http/src/main/java/org/springframework/integration/http/config/IntegrationGraphControllerParser.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828

2929
/**
3030
* The {@link BeanDefinitionParser} for the {@code <int-http:graph-controller>} component.
31+
*
3132
* @author Artem Bilan
3233
*
3334
* @since 4.3
@@ -39,22 +40,15 @@ public class IntegrationGraphControllerParser implements BeanDefinitionParser {
3940

4041
@Override
4142
public BeanDefinition parse(final Element element, ParserContext parserContext) {
42-
if (HttpContextUtils.WEB_MVC_PRESENT) {
43-
this.graphControllerRegistrar.registerBeanDefinitions(
44-
new AnnotationMetadataAdapter() {
45-
46-
@Override
47-
public Map<String, Object> getAnnotationAttributes(String annotationType) {
48-
return Collections.singletonMap("value", element.getAttribute("path"));
49-
}
50-
51-
}, parserContext.getRegistry());
52-
}
53-
else {
54-
parserContext.getReaderContext().warning("The 'IntegrationGraphController' isn't registered " +
55-
"with the application context because" +
56-
" there is no 'org.springframework.web.servlet.DispatcherServlet' in the classpath.", element);
57-
}
43+
this.graphControllerRegistrar.registerBeanDefinitions(
44+
new AnnotationMetadataAdapter() {
45+
46+
@Override
47+
public Map<String, Object> getAnnotationAttributes(String annotationType) {
48+
return Collections.singletonMap("value", element.getAttribute("path"));
49+
}
50+
51+
}, parserContext.getRegistry());
5852

5953
return null;
6054
}

0 commit comments

Comments
 (0)