126.3. 自定义 Groovy Shell

有时,您可能需要在 Groovy 表达式中使用自定义 GroovyShell 实例。为了提供自定义 GroovyShell,可为您的 Camel 注册表添加 org.apache.camel.language.groovy.GroovyShellFactory SPI 接口的实施。例如,在 Spring context…​ 中添加以下内容后

public class CustomGroovyShellFactory implements GroovyShellFactory {
 
  public GroovyShell createGroovyShell(Exchange exchange) {
    ImportCustomizer importCustomizer = new ImportCustomizer();
    importCustomizer.addStaticStars("com.example.Utils");
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.addCompilationCustomizers(importCustomizer);
    return new GroovyShell(configuration);
  }

}

…​Camel 将使用您的自定义 GroovyShell 实例(包含您的自定义静态导入),而不是默认导入。