SpEL 的 Maven 条目作为 ScriptEngine

Maven entry for SpEL as a ScriptEngine

你能告诉我 Maven 依赖项来添加 SpEL - Spring 表达式语言 - 作为我的项目的 ScriptEngine - Spring 中有吗?)

我找到了一些例子:

https://gist.github.com/maggandalf/1380124

https://github.com/melin/starflow/blob/master/src/main/java/com/googlecode/starflow/core/script/spel/SpelScriptEngine.java

示例中的代码展示了如何将 SpEL 包装为 JSR-223 脚本引擎,并通过名称(例如,"spel")使其可供脚本管理器使用。

但我希望它以 Maven 依赖的形式存在。

我不知道我是否理解正确,但试试这个

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.3.3.RELEASE</version>
</dependency>

如果pom.xml只有这个依赖,包中的代码 https://github.com/melin/starflow/blob/master/src/main/java/com/googlecode/starflow/core/script/spel/

应该用 JDK1.8 编译。

(4.3.3.RELEASE如有需要请更换为其他版本)

我刚试过https://github.com/eobermuhlner/spel-scriptengine

您只需要将此添加到您的 pom.xml

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

然后像这样将其与 Hibernate Validator 一起使用:

package org.eu.rubensa.model;

import java.time.Instant;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.ScriptAssert;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "sample")
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@ScriptAssert.List({
    // Month valiadation
    @ScriptAssert(lang = "spel", alias = "_this", script = "#_this.getLower() == null || #_this.getHigher() == null || #_this.getHigher().compareTo(#_this.getLower()) >= 0", reportOn = "finalMonth", message = "{org.eu.rubensa.validation.LowerGreaterThanHigher.message}"),
    // Instant validation
    @ScriptAssert(lang = "spel", alias = "_this", script = "#_this.getStart() == null || #_this.getEnd() == null || #_this.getEnd().compareTo(#_this.getStart()) >= 0", reportOn = "fechaFinPresentacion", message = "{org.eu.rubensa.validation.StartGreaterThanEnd.message}") })
public class SampleEntity {
  @Id
  @Column(name = "id", nullable = false)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sample_seq")
  @SequenceGenerator(name = "sample_seq", sequenceName = "sample_seq", allocationSize = 1)
  private Long id;

  @Column(name = "lower", nullable = false)
  @NotNull
  @Min(1)
  private Integer lower;

  @Column(name = "higher", nullable = false)
  @NotNull
  @Min(1)
  private Integer higher;

  @Column(name = "start", nullable = true)
  private Instant start;

  @Column(name = "end", nullable = true)
  private Instant end;
}