Spring Expression ScriptEngine

Java script engine for Spring Expressions.

Лицензия

Лицензия

Группа

Группа

ch.obermuhlner
Идентификатор

Идентификатор

spel-scriptengine
Последняя версия

Последняя версия

1.0.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

Spring Expression ScriptEngine
Java script engine for Spring Expressions.
Ссылка на сайт

Ссылка на сайт

https://github.com/eobermuhlner/spel-scriptengine
Система контроля версий

Система контроля версий

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

Скачать spel-scriptengine

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
org.springframework : spring-expression jar 5.1.9.RELEASE

test (2)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.12
org.assertj : assertj-core jar 3.11.1

Модули Проекта

Данный проект не имеет модулей.

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

Версии библиотеки

Версия
1.0.0