Advanced — New in 1.2.0

SpEL Scripting

rulii-spring 1.2.0 adds first-class support for Spring Expression Language (SpEL) as a scripting language inside rules. SpEL expressions are compiled, cached, and evaluated against the current RuleContext bindings. No extra configuration is required — the integration is wired automatically by auto-configuration.

How It Works

rulii-spring registers a SpelScriptProcessor under the language name "el". When a script with that language name is evaluated, the processor:

  1. Compiles the expression string using Spring's ExpressionParser (compiled expressions are cached for reuse)
  2. Creates a StandardEvaluationContext with a BindingAccessor that bridges rulii Bindings into SpEL's property resolution
  3. Evaluates the expression and returns the result

Writing SpEL Scripts

All RuleContext bindings are exposed as a single SpEL variable named #ctx. Access any binding through it using standard SpEL property syntax. The # prefix is the standard SpEL notation for variables registered on the evaluation context.

// Check a binding value
"#ctx.age >= 18"

// String comparison
"#ctx.country == 'US'"

// Arithmetic and method calls
"#ctx.price * #ctx.quantity > 1000"
"#ctx.name.toUpperCase() == 'ALICE'"

// Conditional expression
"#ctx.score >= 90 ? 'A' : #ctx.score >= 80 ? 'B' : 'C'"

Using SpEL in Rules (Java API)

// Create a SpEL-backed condition using Script.builder() with language "el"
Script<Boolean> spelScript = Script.builder().build("el", "#ctx.age >= 18 and #ctx.country == 'US'");
Condition condition = Condition.builder().build(spelScript);

// Build the rule
Rule rule = Rule.builder()
    .with("EligibilityRule")
    .given(condition)
    .then(Action.builder().build(Script.builder().build("el", "#ctx.status = 'approved'")))
    .build();

For XML-based rule declaration with SpEL expressions, see XML Rule Definitions. The default expression language for the XML namespace is "el" (SpEL).

Auto-Configuration

RuleConfig automatically creates a SpelScriptProcessorFactory bean and wires a ScriptProcessorRegistry into SpringEnabledRuleContextOptions. Every RuleContext created through the standard factory has SpEL available out of the box.

Using JSR-223 languages

You can still use any JSR-223 language (JavaScript, Groovy, etc.) alongside SpEL. See the core Scripting Support page for details on registering JSR-223 processors.