0

Can you please point me to maven dependency to add the SpEL - Spring Expression Language - as a ScriptEngine to my project - is there any in Spring?)

I've found some examples:

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

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

The code in examples show how to wrap SpEL as a JSR-223 scripting engine and make it available to scripting manager by name (say, "spel").

But I'd like it in a form of maven dependency.

fedd
  • 880
  • 12
  • 39

2 Answers2

0

I don't know if I understand you correctly, but try this

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

If pom.xml has this dependency only, the code in the package https://github.com/melin/starflow/blob/master/src/main/java/com/googlecode/starflow/core/script/spel/

should compile with JDK1.8.

(replace 4.3.3.RELEASE with another version if there is a need).

John Donn
  • 1,718
  • 2
  • 19
  • 45
  • Thanks, sorry, I have this. But it doesn't make it available as a JSR-223 scripting engine. With this in classpath, I still have only JS, Beanshell and JUEL available. The code in examples show how to wrap SpEL as a scripting engine and make it available to scripting manager by name (say, "spel") – fedd Oct 16 '16 at 21:05
  • 1
    There seems to be a related issue, still open, on https://jira.spring.io/browse/SPR-7651. If the code on github is what you want, and the license is OK, it should be possible to package the part of the code you need into your own custom library, and upload it to the central Maven repository. – John Donn Oct 16 '16 at 21:26
0

I've just tried https://github.com/eobermuhlner/spel-scriptengine

You just need too add this to your pom.xml

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

And then use it with Hibernate Validator like this:

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;
}
rubensa
  • 1,150
  • 15
  • 8