spring-framework / org.springframework.scripting.support

Package org.springframework.scripting.support

Types

RefreshableScriptTargetSource

open class RefreshableScriptTargetSource : BeanFactoryRefreshableTargetSource

Subclass of BeanFactoryRefreshableTargetSource that determines whether a refresh is required through the given ScriptFactory.

ResourceScriptSource

open class ResourceScriptSource : ScriptSource

org.springframework.scripting.ScriptSource implementation based on Spring's org.springframework.core.io.Resource abstraction. Loads the script text from the underlying Resource's File or InputStream, and tracks the last-modified timestamp of the file (if possible).

ScriptFactoryPostProcessor

open class ScriptFactoryPostProcessor : InstantiationAwareBeanPostProcessorAdapter, BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, DisposableBean, Ordered

org.springframework.beans.factory.config.BeanPostProcessor that handles org.springframework.scripting.ScriptFactory definitions, replacing each factory with the actual scripted Java object generated by it.

This is similar to the org.springframework.beans.factory.FactoryBean mechanism, but is specifically tailored for scripts and not built into Spring's core container itself but rather implemented as an extension.

NOTE: The most important characteristic of this post-processor is that constructor arguments are applied to the org.springframework.scripting.ScriptFactory instance while bean property values are applied to the generated scripted object. Typically, constructor arguments include a script source locator and potentially script interfaces, while bean property values include references and config values to inject into the scripted object itself.

The following ScriptFactoryPostProcessor will automatically be applied to the two org.springframework.scripting.ScriptFactory definitions below. At runtime, the actual scripted objects will be exposed for "bshMessenger" and "groovyMessenger", rather than the org.springframework.scripting.ScriptFactory instances. Both of those are supposed to be castable to the example's Messenger interfaces here.

<bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/> <bean id="bshMessenger" class="org.springframework.scripting.bsh.BshScriptFactory"> <constructor-arg value="classpath:mypackage/Messenger.bsh"/> <constructor-arg value="mypackage.Messenger"/> <property name="message" value="Hello World!"/> </bean> <bean id="groovyMessenger" class="org.springframework.scripting.groovy.GroovyScriptFactory"> <constructor-arg value="classpath:mypackage/Messenger.groovy"/> <property name="message" value="Hello World!"/> </bean>

NOTE: Please note that the above excerpt from a Spring XML bean definition file uses just the <bean/>-style syntax (in an effort to illustrate using the ScriptFactoryPostProcessor itself). In reality, you would never create a <bean/> definition for a ScriptFactoryPostProcessor explicitly; rather you would import the tags from the 'lang' namespace and simply create scripted beans using the tags in that namespace... as part of doing so, a ScriptFactoryPostProcessor will implicitly be created for you.

The Spring reference documentation contains numerous examples of using tags in the 'lang' namespace; by way of an example, find below a Groovy-backed bean defined using the 'lang:groovy' tag.

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"> <!-- this is the bean definition for the Groovy-backed Messenger implementation --> <lang:groovy id="messenger" script-source="classpath:Messenger.groovy"> <lang:property name="message" value="I Can Do The Frug" /> </lang:groovy> <!-- an otherwise normal bean that will be injected by the Groovy-backed Messenger --> <bean id="bookingService" class="x.y.DefaultBookingService"> <property name="messenger" ref="messenger" /> </bean> </beans>

StandardScriptEvaluator

open class StandardScriptEvaluator : ScriptEvaluator, BeanClassLoaderAware

javax.script (JSR-223) based implementation of Spring's ScriptEvaluator strategy interface.

StandardScriptFactory

open class StandardScriptFactory : ScriptFactory, BeanClassLoaderAware

org.springframework.scripting.ScriptFactory implementation based on the JSR-223 script engine abstraction (as included in Java 6+). Supports JavaScript, Groovy, JRuby and other JSR-223 compliant engines.

Typically used in combination with a org.springframework.scripting.support.ScriptFactoryPostProcessor; see the latter's javadoc for a configuration example.

StandardScriptUtils

abstract class StandardScriptUtils

Common operations for dealing with a JSR-223 ScriptEngine.

StaticScriptSource

open class StaticScriptSource : ScriptSource

Static implementation of the org.springframework.scripting.ScriptSource interface, encapsulating a given String that contains the script source text. Supports programmatic updates of the script String.

Exceptions

StandardScriptEvalException

open class StandardScriptEvalException : RuntimeException

Exception decorating a javax.script.ScriptException coming out of JSR-223 script evaluation, i.e. a javax.script.ScriptEngine#eval call or javax.script.Invocable#invokeMethod / javax.script.Invocable#invokeFunction call.

This exception does not print the Java stacktrace, since the JSR-223 ScriptException results in a rather convoluted text output. From that perspective, this exception is primarily a decorator for a ScriptException root cause passed into an outer exception.