StandardBeanExpressionResolver class

Evaluates the #{...} parts of @Value strings (and of other framework values, such as @ConditionalOnExpression) against the beans of a bean factory.

Every application context installs one, so @Value('#{mailer.from}') works without setup; most applications never touch it. Replace or subclass it to give those expressions more: variables, accessors for classes that are not @Introspected, or a parser with other limits.

The text is parsed as a template (placeholders ${...} have already been resolved by then), and parsed expressions are cached by text. Each bean factory gets one evaluation context, created on first use and kept: its root is a BeanExpressionContext (so #{mailer} is a bean), with @bean references, the environment accessor and methods, BeanFactoryAccessor, the factory's ConversionService as type converter, and the extra accessors and resolvers you pass. Override customizeEvaluationContext to add variables or functions. The default parser accepts expressions up to 10 000 characters, changed with the compile-time define -Dspring.context.expression.maxLength=..., like Spring's property.

// `#answer` becomes available in every `@Value` expression.
class MyResolver extends StandardBeanExpressionResolver {
  MyResolver() : super(propertyAccessors: [userAccessor]);

  @override
  void customizeEvaluationContext(StandardEvaluationContext context) =>
      context.setVariable('answer', 42);
}

final factory = DefaultListableBeanFactory()
  ..beanExpressionResolver = MyResolver();
factory.value<int>('#{#answer + 1}'); // 43

Spring (Java):

beanFactory.setBeanExpressionResolver(
    new StandardBeanExpressionResolver(classLoader) {
      @Override
      protected void customizeEvaluationContext(
          StandardEvaluationContext context) {
        context.setVariable("answer", 42);
      }
    });
Implemented types

Constructors

StandardBeanExpressionResolver({ExpressionParser? parser, ParserContext parserContext = ParserContext.template, List<PropertyAccessor> propertyAccessors = const [], List<MethodResolver> methodResolvers = const []})
Creates a resolver using parser (a SpelExpressionParser limited to maxExpressionLength characters by default) and parserContext; propertyAccessors and methodResolvers are added to every evaluation context, before the built-in ones. An application context passes the accessor and resolver of its own introspector.

Properties

hashCode → int
The hash code for this object.
no setterinherited
parser → ExpressionParser
The parser for @Value strings.
final
parserContext → ParserContext
How strings are parsed: templates with #{...} by default, so text around the expressions is kept.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

customizeEvaluationContext(StandardEvaluationContext context) → void
Called once for the evaluation context of each bean factory, after the built-in setup and before its first evaluation.
evaluate(String value, BeanFactory beanFactory) → Object?
The value of value, a @Value string whose placeholders are already resolved and which may contain #{...}; a string without an expression is returned as it is. Other beans are reachable through beanFactory. The result is then converted to the injection point's type.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

maxExpressionLength() → int
The longest expression the default parser accepts: the value of -Dspring.context.expression.maxLength, or SpelParserConfiguration.defaultMaxExpressionLength (10 000) when it is not defined.

Constants

maxSpelExpressionLengthPropertyName → const String
The name of the compile-time define that sets the longest @Value expression (dart run -Dspring.context.expression.maxLength=20000; Spring's MAX_SPEL_EXPRESSION_LENGTH_PROPERTY_NAME).