127.3. 自定义 Groovy Shell
有时,您可能需要在 Groovy 表达式中使用自定义 GroovyShell 实例。要提供自定义 GroovyShell,请将 org.apache.camel.language.groovy.GroovyShellFactory SPI 接口的实现添加到 Camel registry 中。例如,在将以下 bean 添加到 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 实例(包含您的自定义静态导入),而不是默认导入。