Skip to content

Commit 281d8d5

Browse files
artembilangaryrussell
authored andcommitted
Deprecate Properties as bean for int global props
Having a bean as a `java.util.Properties` is not confusing and may lead to some conflicts in the real application. Plus it is not so easy to configure: need to know all the possible integration properties - bad end-user experience * Make an `IntegrationProperties` as a public POJO for easy configuration of global properties * Deprecate a presence of the `java.util.Properties` * Leave framework-created `integrationGlobalProperties` as a `Properties` instance for backward compatibility * Fix tests to expose an `IntegrationProperties` bean instead of deprecated `Properties` * Fix docs according the change and current recommendations
1 parent aeb43f3 commit 281d8d5

File tree

10 files changed

+314
-85
lines changed

10 files changed

+314
-85
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.PrintWriter;
2121
import java.io.StringWriter;
22-
import java.util.Arrays;
2322
import java.util.HashSet;
24-
import java.util.LinkedList;
25-
import java.util.List;
2623
import java.util.Properties;
2724
import java.util.Set;
2825
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
@@ -328,17 +325,15 @@ private void registerIntegrationProperties() {
328325
if (!this.beanFactory.containsBean(IntegrationContextUtils.INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME)) {
329326
ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(this.classLoader);
330327
try {
331-
Resource[] defaultResources =
332-
resourceResolver.getResources("classpath*:META-INF/spring.integration.default.properties");
333-
Resource[] userResources =
328+
Resource[] resources =
334329
resourceResolver.getResources("classpath*:META-INF/spring.integration.properties");
335330

336-
List<Resource> resources = new LinkedList<>(Arrays.asList(defaultResources));
337-
resources.addAll(Arrays.asList(userResources));
338-
331+
// TODO Revise in favor of 'IntegrationProperties' instance in the next 6.0 version
339332
BeanDefinitionBuilder integrationPropertiesBuilder = BeanDefinitionBuilder
340333
.genericBeanDefinition(PropertiesFactoryBean.class)
341-
.addPropertyValue("locations", resources);
334+
.addPropertyValue("properties", IntegrationProperties.defaults())
335+
.addPropertyValue("locations", resources)
336+
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
342337

343338
this.registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME,
344339
integrationPropertiesBuilder.getBeanDefinition());

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818

1919
import java.util.Properties;
2020

21+
import org.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
23+
import org.jetbrains.annotations.Nullable;
24+
2125
import org.springframework.beans.factory.BeanFactory;
26+
import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
2227
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2328
import org.springframework.beans.factory.config.BeanDefinition;
2429
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -110,6 +115,8 @@ public abstract class IntegrationContextUtils {
110115

111116
public static final String LIST_MESSAGE_HANDLER_FACTORY_BEAN_NAME = "integrationListMessageHandlerMethodFactory";
112117

118+
private static final Log LOGGER = LogFactory.getLog(IntegrationContextUtils.class);
119+
113120
/**
114121
* @param beanFactory BeanFactory for lookup, must not be null.
115122
* @return The {@link MetadataStore} bean whose name is "metadataStore".
@@ -173,15 +180,15 @@ private static <T> T getBeanOfType(BeanFactory beanFactory, String beanName, Cla
173180
return beanFactory.getBean(beanName, type);
174181
}
175182

183+
// TODO Revise in favor of 'IntegrationProperties' instance in the next 6.0 version
176184
/**
177185
* @param beanFactory The bean factory.
178186
* @return the global {@link IntegrationContextUtils#INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME}
179187
* bean from provided {@code #beanFactory}, which represents the merged
180-
* properties values from all 'META-INF/spring.integration.default.properties'
181-
* and 'META-INF/spring.integration.properties'.
182-
* Or user-defined {@link Properties} bean.
188+
* properties values from all 'META-INF/spring.integration.properties'.
189+
* Or user-defined {@link IntegrationProperties} bean.
183190
* May return only {@link IntegrationProperties#defaults()} if there is no
184-
* {@link IntegrationContextUtils#INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME} bean within
191+
* {@link IntegrationContextUtils#INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME} bean in the
185192
* provided {@code #beanFactory} or provided {@code #beanFactory} is null.
186193
*/
187194
public static Properties getIntegrationProperties(BeanFactory beanFactory) {
@@ -191,8 +198,7 @@ public static Properties getIntegrationProperties(BeanFactory beanFactory) {
191198
if (properties == null) {
192199
Properties propertiesToRegister = new Properties();
193200
propertiesToRegister.putAll(IntegrationProperties.defaults());
194-
Properties userProperties =
195-
getBeanOfType(beanFactory, INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME, Properties.class);
201+
Properties userProperties = obtainUserProperties(beanFactory);
196202
if (userProperties != null) {
197203
propertiesToRegister.putAll(userProperties);
198204
}
@@ -215,6 +221,32 @@ public static Properties getIntegrationProperties(BeanFactory beanFactory) {
215221
return properties;
216222
}
217223

224+
@Nullable
225+
private static Properties obtainUserProperties(BeanFactory beanFactory) {
226+
Object userProperties = getBeanOfType(beanFactory, INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME, Object.class);
227+
if (userProperties instanceof Properties) {
228+
if (BeanDefinition.ROLE_INFRASTRUCTURE !=
229+
((BeanDefinitionRegistry) beanFactory)
230+
.getBeanDefinition(INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME)
231+
.getRole()) {
232+
233+
LOGGER.warn("The 'integrationGlobalProperties' bean must be declared as an instance of " +
234+
"'org.springframework.integration.context.IntegrationProperties'. " +
235+
"A 'java.util.Properties' support as a bean is deprecated for " +
236+
"'integrationGlobalProperties' since version 5.5.");
237+
}
238+
return (Properties) userProperties;
239+
}
240+
else if (userProperties instanceof IntegrationProperties) {
241+
return ((IntegrationProperties) userProperties).toProperties();
242+
}
243+
else if (userProperties != null) {
244+
throw new BeanNotOfRequiredTypeException(INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME,
245+
IntegrationProperties.class, userProperties.getClass());
246+
}
247+
return null;
248+
}
249+
218250
/**
219251
* Return a {@link BeanDefinition} with the given name,
220252
* obtained from the given {@link BeanFactory} or one of its parents.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-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.
@@ -294,12 +294,14 @@ protected ApplicationContext getApplicationContext() {
294294
/**
295295
* @see IntegrationContextUtils#getIntegrationProperties(BeanFactory)
296296
* @return The global integration properties.
297+
* @deprecated since version 5.5 in favor of {@link #getIntegrationProperty(String, Class)};
298+
* will be replaced with {@link IntegrationProperties} variant in the next major version.
297299
*/
300+
@Deprecated
298301
protected Properties getIntegrationProperties() {
299302
return this.integrationProperties;
300303
}
301304

302-
303305
protected MessageBuilderFactory getMessageBuilderFactory() {
304306
if (this.messageBuilderFactory == null) {
305307
this.messageBuilderFactory = new DefaultMessageBuilderFactory();

0 commit comments

Comments
 (0)