Top Description Fields Constructors Methods
org.springframework.scripting.support

public Class ScriptFactoryPostProcessor

extends Object
implements SmartInstantiationAwareBeanPostProcessor, BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, DisposableBean, Ordered
Class Inheritance
All Implemented Interfaces
org.springframework.core.Ordered, org.springframework.beans.factory.DisposableBean, org.springframework.context.ResourceLoaderAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor, org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor, org.springframework.beans.factory.config.BeanPostProcessor
Imports
java.util.Map, java.util.concurrent.ConcurrentHashMap, org.apache.commons.logging.Log, .LogFactory, org.springframework.aop.TargetSource, org.springframework.aop.framework.AopInfrastructureBean, .ProxyFactory, org.springframework.aop.support.DelegatingIntroductionInterceptor, org.springframework.asm.Type, org.springframework.beans.BeanUtils, .PropertyValue, .PropertyValues, org.springframework.beans.factory.BeanClassLoaderAware, .BeanCreationException, .BeanCurrentlyInCreationException, .BeanDefinitionStoreException, .BeanFactory, .BeanFactoryAware, .DisposableBean, .FactoryBean, org.springframework.beans.factory.config.BeanDefinition, .ConfigurableBeanFactory, .SmartInstantiationAwareBeanPostProcessor, org.springframework.beans.factory.support.BeanDefinitionValidationException, .DefaultListableBeanFactory, .GenericBeanDefinition, org.springframework.cglib.core.Signature, org.springframework.cglib.proxy.InterfaceMaker, org.springframework.context.ResourceLoaderAware, org.springframework.core.Conventions, .Ordered, org.springframework.core.io.DefaultResourceLoader, .ResourceLoader, org.springframework.lang.Nullable, org.springframework.scripting.ScriptFactory, .ScriptSource, org.springframework.util.Assert, .ClassUtils, .ObjectUtils, .StringUtils

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>
Authors
Juergen Hoeller, Rob Harrop, Rick Evans, Mark Fisher, Sam Brannen
Since
2.0

Field Summary

Modifier and TypeField and Description
private ClassLoader
private ConfigurableBeanFactory
private boolean
private long
public static final String
INLINE_SCRIPT_PREFIX

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

public static final String
LANGUAGE_ATTRIBUTE

The language attribute.

protected final Log
logger

Logger available to subclasses.

public static final String
PROXY_TARGET_CLASS_ATTRIBUTE

The proxyTargetClass attribute.

public static final String
REFRESH_CHECK_DELAY_ATTRIBUTE

The refreshCheckDelay attribute.

private ResourceLoader
private static final String
pack-priv final DefaultListableBeanFactory
private static final String
private final Map<String, ScriptSource>
scriptSourceCache

Map from bean name String to ScriptSource object.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected ScriptSource

Returns:

the ScriptSource instance
convertToScriptSource
(String
the name of the scripted bean
beanName
,
String
the script source locator
scriptSourceLocator
,
ResourceLoader
the ResourceLoader to use (if necessary)
resourceLoader
)

Convert the given script source locator to a ScriptSource instance.

protected Class<?>

Returns:

the merged interface as Class
createCompositeInterface
(Class<?>[]
the interfaces to merge
interfaces
)

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

protected Class<?>

Returns:

the config interface
createConfigInterface
(BeanDefinition
the bean definition (property values etc) to create a config interface for
bd
,
Class<?>[]
the interfaces to check against (might define getters corresponding to the setters we're supposed to generate)
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).

protected Object

Returns:

the generated proxy
createRefreshableProxy
(TargetSource
the refreshable TargetSource
ts
,
Class<?>[]
the proxy interfaces (may be null to indicate proxying of all interfaces implemented by the target class)
interfaces
,
boolean proxyTargetClass)

Create a refreshable proxy for the given AOP TargetSource.

protected BeanDefinition

Returns:

the extracted ScriptFactory bean definition
createScriptedObjectBeanDefinition
(BeanDefinition
the full script bean definition
bd
,
String
the name of the internal ScriptFactory bean
scriptFactoryBeanName
,
ScriptSource
the ScriptSource for the scripted bean
scriptSource
,
Class<?>[]
the interfaces that the scripted bean is supposed to implement
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).

protected BeanDefinition

Returns:

the extracted ScriptFactory bean definition
createScriptFactoryBeanDefinition
(BeanDefinition
the full script bean definition
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).

public void
destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

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

Implements org.springframework.core.Ordered.getOrder.

Get the order value of this object.
protected ScriptSource

Returns:

the corresponding ScriptSource instance
getScriptSource
(String
the name of the scripted bean
beanName
,
String
the script source locator associated with the bean
scriptSourceLocator
)

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

public Object
postProcessBeforeInstantiation(Class<?>
the class of the bean to be instantiated
beanClass
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation.

Apply this BeanPostProcessor before the target bean gets instantiated.
public PropertyValues
postProcessProperties(PropertyValues
the property values that the factory is about to apply (never null)
pvs
,
Object
the bean instance created, but whose properties have not yet been set
bean
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessProperties.

Post-process the given property values before the factory applies them to the given bean.
public Class<?>
predictBeanType(Class<?>
the raw class of the bean
beanClass
,
String
the name of the bean
beanName
)

Overrides default org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor.predictBeanType.

Predict the type of the bean to be eventually returned from this processor's postProcessBeforeInstantiation callback.
protected void
prepareScriptBeans(BeanDefinition
the original bean definition in the main BeanFactory
bd
,
String
the name of the internal ScriptFactory bean
scriptFactoryBeanName
,
String
the name of the internal scripted object bean
scriptedObjectBeanName
)

Prepare the script beans in the internal BeanFactory that this post-processor uses.

protected boolean
protected long

Returns:

the refresh check delay
resolveRefreshCheckDelay
(BeanDefinition
the BeanDefinition to check
beanDefinition
)

Get the refresh check delay for the given ScriptFactory BeanDefinition.

public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.
public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setDefaultProxyTargetClass(boolean
the flag value to set
defaultProxyTargetClass
)

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

public void
setDefaultRefreshCheckDelay(long defaultRefreshCheckDelay)

Set the delay between refresh checks, in milliseconds.

public void
setResourceLoader(ResourceLoader
the ResourceLoader object to be used by this object
resourceLoader
)

Implements org.springframework.context.ResourceLoaderAware.setResourceLoader.

Set the ResourceLoader that this object runs in.

This might be a ResourcePatternResolver, which can be checked through instanceof ResourcePatternResolver.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanClassLoaderback to summary
private ClassLoader beanClassLoader
Annotations
@Nullable
beanFactoryback to summary
private ConfigurableBeanFactory beanFactory
Annotations
@Nullable
defaultProxyTargetClassback to summary
private boolean defaultProxyTargetClass
defaultRefreshCheckDelayback to summary
private long defaultRefreshCheckDelay
INLINE_SCRIPT_PREFIXback to summary
public static final String INLINE_SCRIPT_PREFIX

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_ATTRIBUTEback to summary
public static final String LANGUAGE_ATTRIBUTE

The language attribute.

loggerback to summary
protected final Log logger

Logger available to subclasses.

PROXY_TARGET_CLASS_ATTRIBUTEback to summary
public static final String PROXY_TARGET_CLASS_ATTRIBUTE

The proxyTargetClass attribute.

REFRESH_CHECK_DELAY_ATTRIBUTEback to summary
public static final String REFRESH_CHECK_DELAY_ATTRIBUTE

The refreshCheckDelay attribute.

resourceLoaderback to summary
private ResourceLoader resourceLoader
SCRIPT_FACTORY_NAME_PREFIXback to summary
private static final String SCRIPT_FACTORY_NAME_PREFIX
scriptBeanFactoryback to summary
pack-priv final DefaultListableBeanFactory scriptBeanFactory
SCRIPTED_OBJECT_NAME_PREFIXback to summary
private static final String SCRIPTED_OBJECT_NAME_PREFIX
scriptSourceCacheback to summary
private final Map<String, ScriptSource> scriptSourceCache

Map from bean name String to ScriptSource object.

Constructor Detail

ScriptFactoryPostProcessorback to summary
public ScriptFactoryPostProcessor()

Method Detail

convertToScriptSourceback to summary
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:String

the name of the scripted bean

scriptSourceLocator:String

the script source locator

resourceLoader:ResourceLoader

the ResourceLoader to use (if necessary)

Returns:ScriptSource

the ScriptSource instance

createCompositeInterfaceback to summary
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:Class<?>[]

the interfaces to merge

Returns:Class<?>

the merged interface as Class

See Also
java.lang.reflect.Proxy#getProxyClass
createConfigInterfaceback to summary
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:BeanDefinition

the bean definition (property values etc) to create a config interface for

interfaces:Class<?>[]

the interfaces to check against (might define getters corresponding to the setters we're supposed to generate)

Returns:Class<?>

the config interface

See Also
org.springframework.cglib.proxy.InterfaceMaker, org.springframework.beans.BeanUtils#findPropertyType
createRefreshableProxyback to summary
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass)

Create a refreshable proxy for the given AOP TargetSource.

Parameters
ts:TargetSource

the refreshable TargetSource

interfaces:Class<?>[]

the proxy interfaces (may be null to indicate proxying of all interfaces implemented by the target class)

Returns:Object

the generated proxy

See Also
RefreshableScriptTargetSource
createScriptedObjectBeanDefinitionback to summary
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:BeanDefinition

the full script bean definition

scriptFactoryBeanName:String

the name of the internal ScriptFactory bean

scriptSource:ScriptSource

the ScriptSource for the scripted bean

interfaces:Class<?>[]

the interfaces that the scripted bean is supposed to implement

Returns:BeanDefinition

the extracted ScriptFactory bean definition

See Also
org.springframework.scripting.ScriptFactory#getScriptedObject
createScriptFactoryBeanDefinitionback to summary
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:BeanDefinition

the full script bean definition

Returns:BeanDefinition

the extracted ScriptFactory bean definition

See Also
org.springframework.scripting.ScriptFactory
destroyback to summary
public void destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

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

Annotations
@Override
getOrderback to summary
public int getOrder()

Implements org.springframework.core.Ordered.getOrder.

Doc from org.springframework.core.Ordered.getOrder.

Get the order value of this object.

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

Same order values will result in arbitrary sort positions for the affected objects.

Returns:int

the order value

Annotations
@Override
getScriptSourceback to summary
protected ScriptSource getScriptSource(String beanName, String scriptSourceLocator)

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

Parameters
beanName:String

the name of the scripted bean

scriptSourceLocator:String

the script source locator associated with the bean

Returns:ScriptSource

the corresponding ScriptSource instance

See Also
convertToScriptSource
postProcessBeforeInstantiationback to summary
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName)

Overrides default org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation.

Doc from org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation.

Apply this BeanPostProcessor before the target bean gets instantiated. The returned bean object may be a proxy to use instead of the target bean, effectively suppressing default instantiation of the target bean.

If a non-null object is returned by this method, the bean creation process will be short-circuited. The only further processing applied is the postProcessAfterInitialization callback from the configured BeanPostProcessors.

This callback will be applied to bean definitions with their bean class, as well as to factory-method definitions in which case the returned bean type will be passed in here.

Post-processors may implement the extended SmartInstantiationAwareBeanPostProcessor interface in order to predict the type of the bean object that they are going to return here.

The default implementation returns null.

Parameters
beanClass:Class<?>

the class of the bean to be instantiated

beanName:String

the name of the bean

Returns:Object

the bean object to expose instead of a default instance of the target bean, or null to proceed with default instantiation

Annotations
@Override
postProcessPropertiesback to summary
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)

Overrides default org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessProperties.

Doc from org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor.postProcessProperties.

Post-process the given property values before the factory applies them to the given bean.

The default implementation returns the given pvs as-is.

Parameters
pvs:PropertyValues

the property values that the factory is about to apply (never null)

bean:Object

the bean instance created, but whose properties have not yet been set

beanName:String

the name of the bean

Returns:PropertyValues

the actual property values to apply to the given bean (can be the passed-in PropertyValues instance), or null to skip property population

Annotations
@Override
predictBeanTypeback to summary
public Class<?> predictBeanType(Class<?> beanClass, String beanName)

Overrides default org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor.predictBeanType.

Doc from org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor.predictBeanType.

Predict the type of the bean to be eventually returned from this processor's postProcessBeforeInstantiation callback.

The default implementation returns null. Specific implementations should try to predict the bean type as far as known/cached already, without extra processing steps.

Parameters
beanClass:Class<?>

the raw class of the bean

beanName:String

the name of the bean

Returns:Class<?>

the type of the bean, or null if not predictable

Annotations
@Override
@Nullable
prepareScriptBeansback to summary
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:BeanDefinition

the original bean definition in the main BeanFactory

scriptFactoryBeanName:String

the name of the internal ScriptFactory bean

scriptedObjectBeanName:String

the name of the internal scripted object bean

resolveProxyTargetClassback to summary
protected boolean resolveProxyTargetClass(BeanDefinition beanDefinition)
resolveRefreshCheckDelayback to summary
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 defaultRefreshCheckDelay value is used.

Parameters
beanDefinition:BeanDefinition

the BeanDefinition to check

Returns:long

the refresh check delay

setBeanClassLoaderback to summary
public void setBeanClassLoader(ClassLoader classLoader)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Doc from org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

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 InitializingBean#afterPropertiesSet() method or a custom init-method.

Parameters
classLoader:ClassLoader

the owning class loader

Annotations
@Override
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

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 InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
beanFactory:BeanFactory

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

Annotations
@Override
setDefaultProxyTargetClassback to summary
public void setDefaultProxyTargetClass(boolean defaultProxyTargetClass)

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

Parameters
defaultProxyTargetClass:boolean

the flag value to set

setDefaultRefreshCheckDelayback to summary
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 org.springframework.scripting.ScriptSource indicates that it has been modified.

See Also
org.springframework.scripting.ScriptSource#isModified()
setResourceLoaderback to summary
public void setResourceLoader(ResourceLoader resourceLoader)

Implements org.springframework.context.ResourceLoaderAware.setResourceLoader.

Doc from org.springframework.context.ResourceLoaderAware.setResourceLoader.

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

the ResourceLoader object to be used by this object

Annotations
@Override