Java脚本引擎在哪里使用? [英] Where is the Java scripting engine used?

查看:204
本文介绍了Java脚本引擎在哪里使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Java中有效使用脚本引擎?

How do I effectively use the "Scripting Engine" inside Java?

使用脚本引擎的所有正确用例是什么?

What are all the right use-cases to use the scripting engine?

是否有一个使用Scripting Engine的开源项目?

Is there an open source project using "Scripting Engine"?

有一件事是关闭,功能编程支持是可能的,但它比应用程序要求更具技术用途。

One thing comes to mind is "Closure, Functional programming" support is possible, but it is more of technical use than "Application Requirement".

可配置的插件是可以的。但是仍然有很多模式(访问者,装饰者)在高层次上可以做同样的事情。

Configurable plugins are OK. But still so many patterns (visitor, decorator) on high level can do the same.

我不太了解这个要求......它的使用效率如何在Java EE模式中......它可以与现有模式相辅相成。

I don't know the requirement well... how effectively it could be used in Java EE patterns... where it could complement with the existing patterns.

此外,我希望看到一些商业用例的更多答案。也许就像在销售期间根据会员资格或位置找到产品的复杂折扣。查找复杂算法的排名。特别是在某些情况下为什么不是Java? (或.NET世界中的C#)

Moreover I would like to see more answers with some business usecases. Maybe like finding a complex discount for a product during sale based on membership or location. Finding ranking for a complex algorithm. Especially why not Java in some scenario? (or C# in .NET world)

推荐答案

在Java 6中,内置了脚本引擎支持。例如,

In Java 6, scripting engine support is built in. For example,

    // Create a script engine manager
    ScriptEngineManager factory = new ScriptEngineManager();

    // Create a JavaScript engine
    ScriptEngine engine = factory.getEngineByName("JavaScript");

    // Evaluate JavaScript code from String
    engine.eval("print('Hello, World')");

为什么要使用一个?一些原因:

Why would you use one? Some reasons:


  1. 您希望在Java中使用脚本语言库(例如,您可以通过Java运行的Python库) Jython)

  2. 您希望为客户提供可配置的编程机制,以便他们可以提供简短的代码片段。例如,我过去已经这样做了,允许客户使用JavaScript编写过滤器(例如x< 2&y> 5和z> 10?)。

  3. 你可以实现

  4. 您可以使用更适合该域的语言实现解决方案(例如,通过Clojure使用lambdas),但是依赖于JVM。

  1. you have a library in a scripting language that you want to use in Java (e.g. a Python library that you could run via Jython)
  2. You want to provide a configurable programming mechanism for customers, such that they can provide short code snippets. For example, I've done this in the past allowing customers to write filters using JavaScript (e.g. is x < 2 and y > 5 and z > 10 ?).
  3. You can implement more complex logic in tools like Ant by scripting directly in the configuration file
  4. You can implement solutions in a language more suited to that domain (e.g. using lambdas via Clojure), but maintain your reliance on the JVM.

实现包括Rhino(Javascript的Java实现),Jython(Java Python)等等。

Implementations include Rhino (a Java implementation of Javascript), Jython (a Java Python) and many more.

这篇关于Java脚本引擎在哪里使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆