spring-framework / org.springframework.scripting.support / ScriptFactoryPostProcessor

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>

Author
Juergen Hoeller

Author
Rob Harrop

Author
Rick Evans

Author
Mark Fisher

Since
2.0

Constructors

<init>

ScriptFactoryPostProcessor()

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>

Properties

INLINE_SCRIPT_PREFIX

static val INLINE_SCRIPT_PREFIX: String

The org.springframework.core.io.Resource-style prefix that denotes an inline script.

An inline script is a script that is defined right there in the (typically XML) configuration, as opposed to being defined in an external file.

LANGUAGE_ATTRIBUTE

static val LANGUAGE_ATTRIBUTE: String

PROXY_TARGET_CLASS_ATTRIBUTE

static val PROXY_TARGET_CLASS_ATTRIBUTE: String

REFRESH_CHECK_DELAY_ATTRIBUTE

static val REFRESH_CHECK_DELAY_ATTRIBUTE: String

Functions

destroy

open fun destroy(): Unit

Destroy the inner bean factory (used for scripts) on shutdown.

getOrder

open fun getOrder(): Int

postProcessBeforeInstantiation

open fun postProcessBeforeInstantiation(beanClass: Class<*>, beanName: String): Any

predictBeanType

open fun predictBeanType(beanClass: Class<*>, beanName: String): Class<*>

setBeanClassLoader

open fun setBeanClassLoader(classLoader: ClassLoader): Unit

setBeanFactory

open fun setBeanFactory(beanFactory: BeanFactory): Unit

setDefaultProxyTargetClass

open fun setDefaultProxyTargetClass(defaultProxyTargetClass: Boolean): Unit

Flag to signal that refreshable proxies should be created to proxy the target class not its interfaces.

setDefaultRefreshCheckDelay

open fun setDefaultRefreshCheckDelay(defaultRefreshCheckDelay: Long): Unit

Set the delay between refresh checks, in milliseconds. Default is -1, indicating no refresh checks at all.

Note that an actual refresh will only happen when the org.springframework.scripting.ScriptSource indicates that it has been modified.

setResourceLoader

open fun setResourceLoader(resourceLoader: ResourceLoader): Unit

Inherited Functions

determineCandidateConstructors

open fun determineCandidateConstructors(beanClass: Class<*>, beanName: String): Array<Constructor<*>>

getEarlyBeanReference

open fun getEarlyBeanReference(bean: Any, beanName: String): Any

postProcessAfterInitialization

open fun postProcessAfterInitialization(bean: Any, beanName: String): Any

postProcessAfterInstantiation

open fun postProcessAfterInstantiation(bean: Any, beanName: String): Boolean

postProcessBeforeInitialization

open fun postProcessBeforeInitialization(bean: Any, beanName: String): Any

postProcessPropertyValues

open fun postProcessPropertyValues(pvs: PropertyValues, pds: Array<PropertyDescriptor>, bean: Any, beanName: String): PropertyValues