spring-elspring5

Nested variable reference in SpEL stopped working in Spring-5.x


The foo.bar is 1, the foo.foo is 2, and the bar is "foo". The following construct:

<property name="boo" value="${${bar}.foo}"/>

used to work with Spring-4, setting property boo to 2.

But with Spring-5.x it is not expanded at all -- the property boo is assigned the entire literal expression-string instead.

What's the simplest fix here, without restructuring the underlying Java-code?


Solution

  • The issue you're facing with Spring 5.x is due to stricter handling of nested property placeholders compared to Spring 4.x, which was done to avoid security risks.

    You can enable legacy behavior for property placeholders, for example.

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- others -->
    
        <!-- Enable legacy placeholder behavior -->
        <bean id="propertyAccessor" class="org.springframework.context.expression.BeanExpressionResolver" />
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="beanExpressionResolver" ref="propertyAccessor" />
        </bean>
    
        <!-- Your other configurations -->
        <property name="boo" value="${${bar}.foo}"/>
    </beans>
    

    Test this solution, but keep in mind that maintaining the default stricter behavior is generally better for security and maintainability.