Browse Source

Merge branch '6.0.x'

pull/31194/head
Sam Brannen 1 year ago
parent
commit
154aec7d75
  1. 10
      framework-docs/modules/ROOT/pages/core/expressions/language-ref/types.adoc
  2. 31
      spring-expression/src/main/java/org/springframework/expression/spel/support/StandardEvaluationContext.java
  3. 9
      spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeLocator.java

10
framework-docs/modules/ROOT/pages/core/expressions/language-ref/types.adoc

@ -38,5 +38,15 @@ Kotlin:: @@ -38,5 +38,15 @@ Kotlin::
----
======
[NOTE]
====
If your application or framework manages its own `EvaluationContext`, you may need to
manually configure a `StandardTypeLocator` with a specific `ClassLoader` to ensure that
the SpEL expression parser is able to reliably locate user types.
For example, the `StandardBeanExpressionResolver` in the `spring-context` module
configures a `StandardTypeLocator` using the bean `ClassLoader` of the corresponding
`BeanFactory`.
====

31
spring-expression/src/main/java/org/springframework/expression/spel/support/StandardEvaluationContext.java

@ -40,12 +40,17 @@ import org.springframework.util.Assert; @@ -40,12 +40,17 @@ import org.springframework.util.Assert;
/**
* A powerful and highly configurable {@link EvaluationContext} implementation.
* This context uses standard implementations of all applicable strategies,
* based on reflection to resolve properties, methods and fields.
*
* <p>For a simpler builder-style context variant for data-binding purposes,
* <p>This context uses standard implementations of all applicable strategies,
* based on reflection to resolve properties, methods, and fields. Note, however,
* that you may need to manually configure a {@code StandardTypeLocator} with a
* specific {@link ClassLoader} to ensure that the SpEL expression parser is able
* to reliably locate user types. See {@link #setTypeLocator(TypeLocator)} for
* details.
*
* <p>For a simpler, builder-style context variant for data-binding purposes,
* consider using {@link SimpleEvaluationContext} instead which allows for
* opting into several SpEL features as needed by specific evaluation cases.
* opting into several SpEL features as needed by specific use cases.
*
* @author Andy Clement
* @author Juergen Hoeller
@ -183,11 +188,29 @@ public class StandardEvaluationContext implements EvaluationContext { @@ -183,11 +188,29 @@ public class StandardEvaluationContext implements EvaluationContext {
return this.beanResolver;
}
/**
* Set the {@link TypeLocator} to use to find types, either by short or
* fully-qualified name.
* <p>By default, a {@link StandardTypeLocator} will be used.
* <p><strong>NOTE</strong>: Even if a {@code StandardTypeLocator} is
* sufficient, you may need to manually configure a {@code StandardTypeLocator}
* with a specific {@link ClassLoader} to ensure that the SpEL expression
* parser is able to reliably locate user types.
* @param typeLocator the {@code TypeLocator} to use
* @see StandardTypeLocator#StandardTypeLocator(ClassLoader)
* @see #getTypeLocator()
*/
public void setTypeLocator(TypeLocator typeLocator) {
Assert.notNull(typeLocator, "TypeLocator must not be null");
this.typeLocator = typeLocator;
}
/**
* Get the configured {@link TypeLocator} that will be used to find types,
* either by short or fully-qualified name.
* <p>See {@link #setTypeLocator(TypeLocator)} for further details.
* @see #setTypeLocator(TypeLocator)
*/
@Override
public TypeLocator getTypeLocator() {
if (this.typeLocator == null) {

9
spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeLocator.java

@ -29,7 +29,8 @@ import org.springframework.util.ClassUtils; @@ -29,7 +29,8 @@ import org.springframework.util.ClassUtils;
/**
* A simple implementation of {@link TypeLocator} that uses the default
* {@link ClassLoader} or a supplied {@link ClassLoader} to locate types.
* {@link #StandardTypeLocator() ClassLoader} or a supplied
* {@link #StandardTypeLocator(ClassLoader) ClassLoader} to locate types.
*
* <p>Supports <em>well-known</em> packages, registered as
* {@linkplain #registerImport(String) import prefixes}. If a type cannot be found,
@ -51,6 +52,9 @@ public class StandardTypeLocator implements TypeLocator { @@ -51,6 +52,9 @@ public class StandardTypeLocator implements TypeLocator {
/**
* Create a {@code StandardTypeLocator} for the default {@link ClassLoader}
* (typically, the thread context {@code ClassLoader}).
* <p>Favor {@link #StandardTypeLocator(ClassLoader)} over this constructor
* in order to provide a specific {@link ClassLoader} that is able to reliably
* locate user types.
* @see ClassUtils#getDefaultClassLoader()
*/
public StandardTypeLocator() {
@ -59,6 +63,9 @@ public class StandardTypeLocator implements TypeLocator { @@ -59,6 +63,9 @@ public class StandardTypeLocator implements TypeLocator {
/**
* Create a {@code StandardTypeLocator} for the given {@link ClassLoader}.
* <p>Favor this constructor over {@link #StandardTypeLocator()} in order
* to provide a specific {@link ClassLoader} that is able to reliably locate
* user types.
* @param classLoader the {@code ClassLoader} to delegate to
*/
public StandardTypeLocator(@Nullable ClassLoader classLoader) {

Loading…
Cancel
Save