Using BSF in servlets or applications is also quite simple. In order
to provide an application with scripting support, you need to
import the BSF class hierarchy and instantiate a BSFManager
object. After instantiating the BSFManager, you
register or declare any Java objects to be made available within the
scripting engine. Then call either one of the eval()
or exec() BSFManager methods (depending on whether you want to
evaluate a script and have the value of the evaluation returned, or
execute a script). Alternatively, you can call the
loadScriptingEngine() method in order to get an object
implementing the BSFEngine interface for the desired
scripting language. You can then call the exec() or
eval() methods of BSFEngine to run the script.
Additionally, BSF declares an object named bsf within a
scripting engine's execution context, which represents the
BSFManager that is associated with the scripting engine.
This object provides all of the methods and properties
associated with the BSFManager to the script.
However, the most used method within scripts is usually
lookupBean(), which is used to access objects
in BSF's object registry.
BSFManager内で最も重要なメソッドは:
BSFManager() - BSFManagerのコンストラクタ
eval() - スクリプトを評価し、その値を戻すのに使われる
exec() - スクリプトを実行するのに使われる
loadScriptingEngine() - お望みのスクリプト言語用のBSFEngineを戻すのに使われる
registerBean() - BSFのオブジェクトレジストリにオブジェクトを追加する
lookupBean() - BSFのオブジェクトレジストリからオブジェクトを引っ張り出す
declareBean() - ロードされたスクリプト言語のコンテキスト内に暗黙のオブジェクト(lookupBean()を通じてアクセスする必要の無い)を生成する
その他、使う頻度が比較的少ないBSFManager内のメソッドとしては:
apply() - 匿名の関数を呼び出す際に使われる
compileExpr() - (言語)表現をCodeBufferオブジェクトにコンパイルする際に使われる
compileScript() - 上のcompileExprににているが、スクリプトをCodeBufferオブジェクトにコンパイルする際に使われる
compileApply() - 上述の2つと似ているが、匿名関数をCodeBufferオブジェクトにコンパイルする際に使われる
好奇心旺盛な方向けの話ですが、CodeBufferは、生成されたJavaコードを格納するためにBSFによって提供されるクラスです。
BSFManagerのexec(), eval(),
apply()メソッド(compileExecなどといったcompileのついたメソッドも同様に)は、BSFEngineインターフェースによって表現される対応するメソッドのラッパーとなります。プログラマがloadScriptingEngine()を通じて明白にスクリプトエンジンをロードした場合、BSFEngineに起因するexec()メソッドやeval()メソッドを適切に使うことが出来ます。