mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-17 16:39:29 +00:00
Prior to this commit, SpelParserConfiguration exposed 8 overloaded constructors that accumulated over time as new configuration options were introduced (auto-grow support since 3.0, maximumExpressionLength in 5.2.25, maximumOperations in 6.2.19, and maximumBigPowerBits in 7.0.9), culminating in an 8-parameter constructor. This made call sites hard to read due to unlabeled sequences of booleans and ints, and it forced users who wanted to override a single setting to also supply every other value explicitly. To address that, this commit introduces a builder API in SpelParserConfiguration, following the pattern already established by SimpleEvaluationContext's builder API. Specifically, SpelParserConfiguration.builder() returns a Builder that is pre-populated with the same defaults as the no-arg constructor, including the SpringProperties-driven overrides for the default compiler mode, maximum operations, and maximum big-power bits -- the latter two are only resolved lazily in build(), so that overriding them via the builder never triggers an unnecessary SpringProperties lookup. Each property has a dedicated, named setter (compilerMode(), compilerClassLoader(), maximumAutoGrowSize(), maximumExpressionLength(), maximumOperations(), maximumBigPowerBits()), and the two auto-grow flags are exposed as simple no-arg opt-ins (autoGrowNullReferences(), autoGrowCollections()) since they both default to false. build() delegates to the existing canonical constructor, so validation and defaults remain centralized in one place. In addition, a new SpelParserConfiguration.withDefaults() factory method has been introduced as shorthand for SpelParserConfiguration.builder().build(), for the common case where none of the builder's defaults need to be overridden. As the one deliberate exception to matching the no-arg constructor's defaults, the builder defaults maximumAutoGrowSize to 256 -- aligned with DataBinder.DEFAULT_AUTO_GROW_COLLECTION_LIMIT -- rather than the constructors' Integer.MAX_VALUE. The constructors keep their legacy default for backward compatibility, but the builder is a new, opt-in API that is not bound by that compatibility contract. This change is purely additive: none of the existing constructors have been modified or deprecated. Deprecating those constructors in favor of the builder is being deferred to 7.1, since new deprecations should not be introduced in a patch release. In the meantime, the Javadoc for the constructors and for the SPRING_EXPRESSION_*_PROPERTY_NAME constants has been updated to favor the builder (or a specific Builder setter) instead of the constructors, and the class-level Javadoc now states that the constructors are planned to be deprecated in favor of the builder as of Spring Framework 7.1. SpelExpressionParser's no-arg constructor, ExpressionState's two convenience constructors, and StandardBeanExpressionResolver's ClassLoader-based constructor have all been switched from the SpelParserConfiguration constructors to the builder (or withDefaults()). This is behaviorally identical in every case: autoGrowCollections remains false at each of those call sites, and maximumAutoGrowSize -- the only property whose default differs between the constructors and the builder -- has no effect when autoGrowCollections is false. Tests have been added in a new SpelParserConfigurationTests class to verify that the builder's defaults match the no-arg constructor (with the one intentional maximumAutoGrowSize exception called out above), that custom values are applied correctly, and that invalid values are rejected. The nested LegacyConstructorTests class provides regression coverage for each of the legacy constructors, consolidating their usage in tests to a single class -- which will keep any future deprecation warnings confined to this class -- and documents that, unlike the builder, the canonical constructor does not (yet) reject a negative maximumAutoGrowSize. The remaining incidental usages of the SpelParserConfiguration constructors throughout EvaluationTests, IndexingTests, SpelCompilationCoverageTests, SpelReproTests, and SpelCompilerTests have been converted to use the builder. Furthermore, the reference documentation has been updated to recommend the builder and withDefaults() over the constructors, both in prose and in the Java/Kotlin examples. Closes gh-37187