java.lang.Object
   ↳ org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
     ↳ org.springframework.scripting.support.ScriptFactoryPostProcessor

Class Overview

BeanPostProcessor that handles ScriptFactory definitions, replacing each factory with the actual scripted Java object generated by it.

This is similar to the 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 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 ScriptFactory definitions below. At runtime, the actual scripted objects will be exposed for "bshMessenger" and "groovyMessenger", rather than the 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.bsh.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>

Summary

Constants
String INLINE_SCRIPT_PREFIX The Resource-style prefix that denotes an inline script.
[Expand]
Inherited Constants
From interface org.springframework.core.Ordered
Fields
public static final String REFRESH_CHECK_DELAY_ATTRIBUTE
protected final Log logger Logger available to subclasses
Public Constructors
ScriptFactoryPostProcessor()
Public Methods
void destroy()
Destroy the inner bean factory (used for scripts) on shutdown.
int getOrder()
Return the order value of this object, with a higher value meaning greater in terms of sorting.
Object postProcessBeforeInstantiation(Class beanClass, String beanName)
Class predictBeanType(Class beanClass, String beanName)
Predict the type of the bean to be eventually returned from this processor's postProcessBeforeInstantiation(Class, String) callback.
void setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the bean class loader to a bean instance.
void setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.
void setDefaultRefreshCheckDelay(long defaultRefreshCheckDelay)
Set the delay between refresh checks, in milliseconds.
void setResourceLoader(ResourceLoader resourceLoader)
Set the ResourceLoader that this object runs in.
Protected Methods
ScriptSource convertToScriptSource(String beanName, String scriptSourceLocator, ResourceLoader resourceLoader)
Convert the given script source locator to a ScriptSource instance.
Class createCompositeInterface(Class[] interfaces)
Create a composite interface Class for the given interfaces, implementing the given interfaces in one single Class.
Class createConfigInterface(BeanDefinition bd, Class[] interfaces)
Create a config interface for the given bean definition, defining setter methods for the defined property values as well as an init method and a destroy method (if defined).
Object createRefreshableProxy(TargetSource ts, Class[] interfaces)
Create a refreshable proxy for the given AOP TargetSource.
BeanDefinition createScriptFactoryBeanDefinition(BeanDefinition bd)
Create a ScriptFactory bean definition based on the given script definition, extracting only the definition data that is relevant for the ScriptFactory (that is, only bean class and constructor arguments).
BeanDefinition createScriptedObjectBeanDefinition(BeanDefinition bd, String scriptFactoryBeanName, ScriptSource scriptSource, Class[] interfaces)
Create a bean definition for the scripted object, based on the given script definition, extracting the definition data that is relevant for the scripted object (that is, everything but bean class and constructor arguments).
ScriptSource getScriptSource(String beanName, String scriptSourceLocator)
Obtain a ScriptSource for the given bean, lazily creating it if not cached already.
void prepareScriptBeans(BeanDefinition bd, String scriptFactoryBeanName, String scriptedObjectBeanName)
Prepare the script beans in the internal BeanFactory that this post-processor uses.
long resolveRefreshCheckDelay(BeanDefinition beanDefinition)
Get the refresh check delay for the given ScriptFactory BeanDefinition.
[Expand]
Inherited Methods
From class org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
From class java.lang.Object
From interface org.springframework.beans.factory.BeanClassLoaderAware
From interface org.springframework.beans.factory.BeanFactoryAware
From interface org.springframework.beans.factory.DisposableBean
From interface org.springframework.beans.factory.config.BeanPostProcessor
From interface org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor
From interface org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor
From interface org.springframework.context.ResourceLoaderAware
From interface org.springframework.core.Ordered

Constants

public static final String INLINE_SCRIPT_PREFIX

The 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.

Constant Value: "inline:"

Fields

public static final String REFRESH_CHECK_DELAY_ATTRIBUTE

protected final Log logger

Logger available to subclasses

Public Constructors

public ScriptFactoryPostProcessor ()

Public Methods

public void destroy ()

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

public int getOrder ()

Return the order value of this object, with a higher value meaning greater in terms of sorting.

Normally starting with 0, with Integer.MAX_VALUE indicating the greatest value. Same order values will result in arbitrary positions for the affected objects.

Higher values can be interpreted as lower priority. As a consequence, the object with the lowest value has highest priority (somewhat analogous to Servlet "load-on-startup" values).

Returns
  • the order value

public Object postProcessBeforeInstantiation (Class beanClass, String beanName)

public Class predictBeanType (Class beanClass, String beanName)

Predict the type of the bean to be eventually returned from this processor's postProcessBeforeInstantiation(Class, String) callback.

Parameters
beanClass the raw class of the bean
beanName the name of the bean
Returns
  • the type of the bean, or null if not predictable

public void setBeanClassLoader (ClassLoader classLoader)

Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's afterPropertiesSet() method or a custom init-method.

Parameters
classLoader the owning class loader; may be null in which case a default ClassLoader must be used, for example the ClassLoader obtained via getDefaultClassLoader()

public void setBeanFactory (BeanFactory beanFactory)

Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as afterPropertiesSet() or a custom init-method.

Parameters
beanFactory owning BeanFactory (never null). The bean can immediately call methods on the factory.

public void setDefaultRefreshCheckDelay (long defaultRefreshCheckDelay)

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 ScriptSource indicates that it has been modified.

See Also

public void setResourceLoader (ResourceLoader resourceLoader)

Set the ResourceLoader that this object runs in.

This might be a ResourcePatternResolver, which can be checked through instanceof ResourcePatternResolver. See also the ResourcePatternUtils.getResourcePatternResolver method.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked before ApplicationContextAware's setApplicationContext.

Parameters
resourceLoader ResourceLoader object to be used by this object

Protected Methods

protected ScriptSource convertToScriptSource (String beanName, String scriptSourceLocator, ResourceLoader resourceLoader)

Convert the given script source locator to a ScriptSource instance.

By default, supported locators are Spring resource locations (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") and inline scripts ("inline:myScriptText...").

Parameters
beanName the name of the scripted bean
scriptSourceLocator the script source locator
resourceLoader the ResourceLoader to use (if necessary)
Returns
  • the ScriptSource instance

protected Class createCompositeInterface (Class[] interfaces)

Create a composite interface Class for the given interfaces, implementing the given interfaces in one single Class.

The default implementation builds a JDK proxy class for the given interfaces.

Parameters
interfaces the interfaces to merge
Returns
  • the merged interface as Class

protected Class createConfigInterface (BeanDefinition bd, Class[] interfaces)

Create a config interface for the given bean definition, defining setter methods for the defined property values as well as an init method and a destroy method (if defined).

This implementation creates the interface via CGLIB's InterfaceMaker, determining the property types from the given interfaces (as far as possible).

Parameters
bd the bean definition (property values etc) to create a config interface for
interfaces the interfaces to check against (might define getters corresponding to the setters we're supposed to generate)
Returns
  • the config interface
See Also

protected Object createRefreshableProxy (TargetSource ts, Class[] interfaces)

Create a refreshable proxy for the given AOP TargetSource.

Parameters
ts the refreshable TargetSource
interfaces the proxy interfaces (may be null to indicate proxying of all interfaces implemented by the target class)
Returns
  • the generated proxy

protected BeanDefinition createScriptFactoryBeanDefinition (BeanDefinition bd)

Create a ScriptFactory bean definition based on the given script definition, extracting only the definition data that is relevant for the ScriptFactory (that is, only bean class and constructor arguments).

Parameters
bd the full script bean definition
Returns
  • the extracted ScriptFactory bean definition
See Also

protected BeanDefinition createScriptedObjectBeanDefinition (BeanDefinition bd, String scriptFactoryBeanName, ScriptSource scriptSource, Class[] interfaces)

Create a bean definition for the scripted object, based on the given script definition, extracting the definition data that is relevant for the scripted object (that is, everything but bean class and constructor arguments).

Parameters
bd the full script bean definition
scriptFactoryBeanName the name of the internal ScriptFactory bean
scriptSource the ScriptSource for the scripted bean
interfaces the interfaces that the scripted bean is supposed to implement
Returns
  • the extracted ScriptFactory bean definition

protected ScriptSource getScriptSource (String beanName, String scriptSourceLocator)

Obtain a ScriptSource for the given bean, lazily creating it if not cached already.

Parameters
beanName the name of the scripted bean
scriptSourceLocator the script source locator associated with the bean
Returns
  • the corresponding ScriptSource instance

protected void prepareScriptBeans (BeanDefinition bd, String scriptFactoryBeanName, String scriptedObjectBeanName)

Prepare the script beans in the internal BeanFactory that this post-processor uses. Each original bean definition will be split into a ScriptFactory definition and a scripted object definition.

Parameters
bd the original bean definition in the main BeanFactory
scriptFactoryBeanName the name of the internal ScriptFactory bean
scriptedObjectBeanName the name of the internal scripted object bean

protected long resolveRefreshCheckDelay (BeanDefinition beanDefinition)

Get the refresh check delay for the given ScriptFactory BeanDefinition. If the BeanDefinition has a metadata attribute under the key REFRESH_CHECK_DELAY_ATTRIBUTE which is a valid Number type, then this value is used. Otherwise, the the #defaultRefreshCheckDelay value is used.

Parameters
beanDefinition the BeanDefinition to check
Returns
  • the refresh check delay