Spring Expression ScriptEngine

Java script engine for Spring Expressions.

License

License

GroupId

GroupId

ch.obermuhlner
ArtifactId

ArtifactId

spel-scriptengine
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Expression ScriptEngine
Java script engine for Spring Expressions.
Project URL

Project URL

https://github.com/eobermuhlner/spel-scriptengine
Source Code Management

Source Code Management

https://github.com/eobermuhlner/spel-scriptengine/

Download spel-scriptengine

How to add to project

<!-- https://jarcasting.com/artifacts/ch.obermuhlner/spel-scriptengine/ -->
<dependency>
    <groupId>ch.obermuhlner</groupId>
    <artifactId>spel-scriptengine</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/ch.obermuhlner/spel-scriptengine/
implementation 'ch.obermuhlner:spel-scriptengine:1.0.0'
// https://jarcasting.com/artifacts/ch.obermuhlner/spel-scriptengine/
implementation ("ch.obermuhlner:spel-scriptengine:1.0.0")
'ch.obermuhlner:spel-scriptengine:jar:1.0.0'
<dependency org="ch.obermuhlner" name="spel-scriptengine" rev="1.0.0">
  <artifact name="spel-scriptengine" type="jar" />
</dependency>
@Grapes(
@Grab(group='ch.obermuhlner', module='spel-scriptengine', version='1.0.0')
)
libraryDependencies += "ch.obermuhlner" % "spel-scriptengine" % "1.0.0"
[ch.obermuhlner/spel-scriptengine "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
org.springframework : spring-expression jar 5.1.9.RELEASE

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 3.11.1

Project Modules

There are no modules declared in this project.

Build Status Code Coverage Quality Gate Status Open Issues Last Commits Maven Central - spel-scriptengine

Spring Expression Language (SpEL)

The Spring Expression Language (SpEL) is a powerful expression language that supports querying and manipulating an object graph at runtime.

Refer to the 5.1.x Spring Documentation for details.

Using Spring Expression Language scripting engine in your projects

To use the Spring Expression Language scripting you can either download the newest version of the .jar file from the published releases on Github or use the following dependency to Maven Central in your build script (please verify the version number to be the newest release):

Use Spring Expression Language scripting engine in Maven build

<dependency>
  <groupId>ch.obermuhlner</groupId>
  <artifactId>spel-scriptengine</artifactId>
  <version>1.0.0</version>
</dependency>

Use Spring Expression Language scripting engine in Gradle build

repositories {
  mavenCentral()
}

dependencies {
  compile 'ch.obermuhlner:spel-scriptengine:1.0.0'
}

Simple usage

The following example shows how to execute a simple SpEL script with variables:

try {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("spel");

    engine.put("inputA", 2);
    engine.put("inputB", 3);
    engine.put("output", 0);

    String script = "" +
            "#output = #inputA + #inputB";

    Object result = engine.eval(script);
    System.out.println("Result: " + result);

    Object output = engine.get("output");
    System.out.println("Output Variable: " + output);
} catch (ScriptException e) {
    e.printStackTrace();
}

The console output shows that the output variable was modified by the script:

Result: 5
Output Variable: 5

Since SpEL has the concept of a root object that is passed into expression the SpringExpressionScriptEngine provides a special variable ROOT.

public class Person {
    public String name;
    public int birthYear;

    @Override
    public String toString() {
        return "Person{name=" + name + ", birthYear=" + birthYear + "}";
    }
}
try {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("spel");

    Person person = new Person();
    person.name = "Eric";
    person.birthYear = 1967;
    engine.put(SpringExpressionScriptEngine.ROOT, person);

    String script = "" +
            "name+birthYear";

    Object result = engine.eval(script);
    System.out.println("Result: " + result);
} catch (ScriptException e) {
    e.printStackTrace();
}
Result: Eric1967

Compiling

The SpringExpressionScriptEngine implements the Compilable interface.

You can compile a script into a CompiledScript and execute it multiple times with different bindings.

try {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("spel");
    Compilable compiler = (Compilable) engine;

    CompiledScript compiledScript = compiler.compile("#alpha + #beta");

    {
        Bindings bindings = engine.createBindings();

        bindings.put("alpha", 2);
        bindings.put("beta", 3);
        Object result = compiledScript.eval(bindings);
        System.out.println("Result (Integer): " + result);
    }

    {
        Bindings bindings = engine.createBindings();

        bindings.put("alpha", "aaa");
        bindings.put("beta", "bbb");
        Object result = compiledScript.eval(bindings);
        System.out.println("Result (String): " + result);
    }
} catch (ScriptException e) {
    e.printStackTrace();
}

The console output shows that the same compiled script was able to run with different bindings, which where even of different runtime types.

Result (Integer): 5
Result (String): aaabbb

Versions

Version
1.0.0