https://github.com/igapyon/simple-spel
Simple SpEL sample
https://github.com/igapyon/simple-spel
Last synced: over 1 year ago
JSON representation
Simple SpEL sample
- Host: GitHub
- URL: https://github.com/igapyon/simple-spel
- Owner: igapyon
- License: apache-2.0
- Created: 2020-05-15T10:58:11.000Z (about 6 years ago)
- Default Branch: devel
- Last Pushed: 2022-07-07T19:40:12.000Z (almost 4 years ago)
- Last Synced: 2025-01-06T17:15:49.217Z (over 1 year ago)
- Language: Java
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simple-spel
SpEL を自前プログラムに組み込んで利用する方法のシンプルサンプルです。
ちょっとしたプログラミング言語を自前プログラムに組み込みたい時に、SpEL を自前プログラムに組み込んで利用するのが良い方法の場合があります。
```java
StandardEvaluationContext context = new StandardEvaluationContext();
try {
// 標準評価に関数を追加.
context.registerFunction("Abcdefg", SimpleSpelSample01.class.getDeclaredMethod( //
"abcdefg", //
new Class[] { Integer.class, Integer.class }));
} catch (NoSuchMethodException | SecurityException e) {
throw new IllegalArgumentException("Unexpected syntax error:" + e.toString());
}
ExpressionParser parser = new SpelExpressionParser();
try {
Expression exp = parser.parseExpression(inputString);
Integer result = exp.getValue(context, Integer.class);
System.err.println("result: " + result);
} catch (SpelParseException e) {
throw new IllegalArgumentException("与えられたSpEL式が不正です。[" + inputString + "]:" + e.toString());
}
}
```