Spel的Maven条目作为ScriptEngine

时间:2016-10-16 20:09:22

标签: spring spring-el jsr223

你能指点我maven依赖来添加SpEL - Spring Expression Language - 作为我的项目的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依赖的形式。

2 个答案:

答案 0 :(得分:0)

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

<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替换为另一个版本。)

答案 1 :(得分:0)

我刚刚试过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;
}