Skip to content

Commit 9c890ea

Browse files
artembilangaryrussell
authored andcommitted
Fix SpelPropertyAccessorRegistrar
The `SpelPropertyAccessorRegistrar` ctor and `add(PropertyAccessor... propertyAccessors)` method use wrong variable to build a key for the provided `PropertyAccessor`. Therefore during iteration the next `PropertyAccessor` overrides the previous and we end up just only with one instance in the registry * Extract `private static obtainAccessorKey()` method to build decapitalized key for the `PropertyAccessor` * Change the map store to the `LinkedHashMap` to keep order of the provided `PropertyAccessor` s * Modify `EnableIntegrationTests` to ensure that we support several `PropertyAccessor` s and in the proper order **Cherry-pick to 4.3.x**
1 parent 7187da6 commit 9c890ea

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

spring-integration-core/src/main/java/org/springframework/integration/expression/SpelPropertyAccessorRegistrar.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 the original author or authors.
2+
* Copyright 2017-2018 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.
@@ -16,7 +16,8 @@
1616

1717
package org.springframework.integration.expression;
1818

19-
import java.util.HashMap;
19+
import java.beans.Introspector;
20+
import java.util.LinkedHashMap;
2021
import java.util.Map;
2122

2223
import org.springframework.expression.PropertyAccessor;
@@ -34,7 +35,7 @@
3435
*/
3536
public class SpelPropertyAccessorRegistrar {
3637

37-
private final Map<String, PropertyAccessor> propertyAccessors = new HashMap<String, PropertyAccessor>();
38+
private final Map<String, PropertyAccessor> propertyAccessors = new LinkedHashMap<String, PropertyAccessor>();
3839

3940
public SpelPropertyAccessorRegistrar() {
4041
}
@@ -48,7 +49,7 @@ public SpelPropertyAccessorRegistrar() {
4849
public SpelPropertyAccessorRegistrar(PropertyAccessor... propertyAccessors) {
4950
Assert.notEmpty(propertyAccessors, "'propertyAccessors' must not be empty");
5051
for (PropertyAccessor propertyAccessor : propertyAccessors) {
51-
this.propertyAccessors.put(propertyAccessors.getClass().getSimpleName(), propertyAccessor);
52+
this.propertyAccessors.put(obtainAccessorKey(propertyAccessor), propertyAccessor);
5253
}
5354
}
5455

@@ -95,9 +96,13 @@ public SpelPropertyAccessorRegistrar add(String name, PropertyAccessor propertyA
9596
public SpelPropertyAccessorRegistrar add(PropertyAccessor... propertyAccessors) {
9697
Assert.notEmpty(propertyAccessors, "'propertyAccessors' must not be empty");
9798
for (PropertyAccessor propertyAccessor : propertyAccessors) {
98-
this.propertyAccessors.put(propertyAccessors.getClass().getSimpleName(), propertyAccessor);
99+
this.propertyAccessors.put(obtainAccessorKey(propertyAccessor), propertyAccessor);
99100
}
100101
return this;
101102
}
102103

104+
private static String obtainAccessorKey(PropertyAccessor propertyAccessor) {
105+
return Introspector.decapitalize(propertyAccessor.getClass().getSimpleName());
106+
}
107+
103108
}

spring-integration-core/src/test/java/org/springframework/integration/configuration/EnableIntegrationTests.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2018 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,9 +69,12 @@
6969
import org.springframework.context.annotation.ComponentScan;
7070
import org.springframework.context.annotation.Configuration;
7171
import org.springframework.context.annotation.ImportResource;
72+
import org.springframework.context.expression.EnvironmentAccessor;
73+
import org.springframework.context.expression.MapAccessor;
7274
import org.springframework.core.convert.converter.Converter;
7375
import org.springframework.core.serializer.support.SerializingConverter;
7476
import org.springframework.expression.EvaluationContext;
77+
import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
7578
import org.springframework.integration.annotation.Aggregator;
7679
import org.springframework.integration.annotation.BridgeFrom;
7780
import org.springframework.integration.annotation.BridgeTo;
@@ -694,7 +697,11 @@ public void testSourcePollingChannelAdapterOutputChannelLateBinding() {
694697
public void testIntegrationEvaluationContextCustomization() {
695698
EvaluationContext evaluationContext = this.context.getBean(EvaluationContext.class);
696699
List<?> propertyAccessors = TestUtils.getPropertyValue(evaluationContext, "propertyAccessors", List.class);
700+
assertEquals(4, propertyAccessors.size());
697701
assertThat(propertyAccessors.get(0), instanceOf(JsonPropertyAccessor.class));
702+
assertThat(propertyAccessors.get(1), instanceOf(EnvironmentAccessor.class));
703+
assertThat(propertyAccessors.get(2), instanceOf(MapAccessor.class));
704+
assertThat(propertyAccessors.get(3), instanceOf(ReflectivePropertyAccessor.class));
698705
Map<?, ?> variables = TestUtils.getPropertyValue(evaluationContext, "variables", Map.class);
699706
Object testSpelFunction = variables.get("testSpelFunction");
700707
assertEquals(ClassUtils.getStaticMethod(TestSpelFunction.class, "bar", Object.class), testSpelFunction);
@@ -1109,7 +1116,7 @@ public SpelFunctionFactoryBean testSpelFunction() {
11091116

11101117
@Bean
11111118
public SpelPropertyAccessorRegistrar spelPropertyAccessorRegistrar() {
1112-
return new SpelPropertyAccessorRegistrar(new JsonPropertyAccessor());
1119+
return new SpelPropertyAccessorRegistrar(new JsonPropertyAccessor(), new EnvironmentAccessor());
11131120
}
11141121

11151122
}

0 commit comments

Comments
 (0)