Class ScriptFactoryPostProcessor
- java.lang.Object
- org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
- org.springframework.scripting.support.ScriptFactoryPostProcessor
- All Implemented Interfaces:
Aware,BeanClassLoaderAware,BeanFactoryAware,BeanPostProcessor,InstantiationAwareBeanPostProcessor,SmartInstantiationAwareBeanPostProcessor,DisposableBean,ResourceLoaderAware,Ordered
public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, DisposableBean, Ordered
BeanPostProcessorthat handlesScriptFactorydefinitions, replacing each factory with the actual scripted Java object generated by it.This is similar to the
FactoryBeanmechanism, 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
ScriptFactoryinstance 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
ScriptFactoryPostProcessorwill automatically be applied to the twoScriptFactorydefinitions below. At runtime, the actual scripted objects will be exposed for "bshMessenger" and "groovyMessenger", rather than theScriptFactoryinstances. Both of those are supposed to be castable to the example'sMessengerinterfaces 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
ScriptFactoryPostProcessoritself). In reality, you would never create a <bean/> definition for aScriptFactoryPostProcessorexplicitly; 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, aScriptFactoryPostProcessorwill 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>- Since:
- 2.0
- Author:
- Juergen Hoeller, Rob Harrop, Rick Evans, Mark Fisher
Field Summary
Fields Modifier and Type Field Description static StringINLINE_SCRIPT_PREFIXTheResource-style prefix that denotes an inline script.static StringLANGUAGE_ATTRIBUTEprotected LogloggerLogger available to subclassesstatic StringPROXY_TARGET_CLASS_ATTRIBUTEstatic StringREFRESH_CHECK_DELAY_ATTRIBUTEFields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor Summary
Constructors Constructor Description ScriptFactoryPostProcessor()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ScriptSourceconvertToScriptSource(String beanName, String scriptSourceLocator, ResourceLoader resourceLoader)Convert the given script source locator to a ScriptSource instance.protected Class<?>createCompositeInterface(Class<?>[] interfaces)Create a composite interface Class for the given interfaces, implementing the given interfaces in one single 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).protected ObjectcreateRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass)Create a refreshable proxy for the given AOP TargetSource.protected BeanDefinitioncreateScriptedObjectBeanDefinition(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).protected BeanDefinitioncreateScriptFactoryBeanDefinition(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).voiddestroy()Destroy the inner bean factory (used for scripts) on shutdown.intgetOrder()Get the order value of this object.protected ScriptSourcegetScriptSource(String beanName, String scriptSourceLocator)Obtain a ScriptSource for the given bean, lazily creating it if not cached already.ObjectpostProcessBeforeInstantiation(Class<?> beanClass, String beanName)Apply this BeanPostProcessor before the target bean gets instantiated.Class<?>predictBeanType(Class<?> beanClass, String beanName)Predict the type of the bean to be eventually returned from this processor'sInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)callback.protected voidprepareScriptBeans(BeanDefinition bd, String scriptFactoryBeanName, String scriptedObjectBeanName)Prepare the script beans in the internal BeanFactory that this post-processor uses.protected booleanresolveProxyTargetClass(BeanDefinition beanDefinition)protected longresolveRefreshCheckDelay(BeanDefinition beanDefinition)Get the refresh check delay for the givenScriptFactoryBeanDefinition.voidsetBeanClassLoader(ClassLoader classLoader)Callback that supplies the beanclass loaderto a bean instance.voidsetBeanFactory(BeanFactory beanFactory)Callback that supplies the owning factory to a bean instance.voidsetDefaultProxyTargetClass(boolean defaultProxyTargetClass)Flag to signal that refreshable proxies should be created to proxy the target class not its interfaces.voidsetDefaultRefreshCheckDelay(long defaultRefreshCheckDelay)Set the delay between refresh checks, in milliseconds.voidsetResourceLoader(ResourceLoader resourceLoader)Set the ResourceLoader that this object runs in.Methods inherited from class org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter
determineCandidateConstructors, getEarlyBeanReference, postProcessAfterInitialization, postProcessAfterInstantiation, postProcessBeforeInitialization, postProcessPropertyValues
Field Detail
INLINE_SCRIPT_PREFIX
public static final String INLINE_SCRIPT_PREFIX
TheResource-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.
- See Also:
- Constant Field Values
REFRESH_CHECK_DELAY_ATTRIBUTE
public static final String REFRESH_CHECK_DELAY_ATTRIBUTE
PROXY_TARGET_CLASS_ATTRIBUTE
public static final String PROXY_TARGET_CLASS_ATTRIBUTE
LANGUAGE_ATTRIBUTE
public static final String LANGUAGE_ATTRIBUTE
Constructor Detail
ScriptFactoryPostProcessor
public ScriptFactoryPostProcessor()
Method Detail
setDefaultRefreshCheckDelay
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
ScriptSourceindicates that it has been modified.- See Also:
ScriptSource.isModified()
setDefaultProxyTargetClass
public void setDefaultProxyTargetClass(boolean defaultProxyTargetClass)
Flag to signal that refreshable proxies should be created to proxy the target class not its interfaces.- Parameters:
defaultProxyTargetClass- the flag value to set
setBeanClassLoader
public void setBeanClassLoader(ClassLoader classLoader)
Description copied from interface:BeanClassLoaderAwareCallback that supplies the beanclass loaderto a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean'sInitializingBean.afterPropertiesSet()method or a custom init-method.- Specified by:
setBeanClassLoaderin interfaceBeanClassLoaderAware- Parameters:
classLoader- the owning class loader; may benullin which case a defaultClassLoadermust be used, for example theClassLoaderobtained viaClassUtils.getDefaultClassLoader()
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
Description copied from interface:BeanFactoryAwareCallback 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.- Specified by:
setBeanFactoryin interfaceBeanFactoryAware- Parameters:
beanFactory- owning BeanFactory (nevernull). The bean can immediately call methods on the factory.- See Also:
BeanInitializationException
setResourceLoader
public void setResourceLoader(ResourceLoader resourceLoader)
Description copied from interface:ResourceLoaderAwareSet the ResourceLoader that this object runs in.This might be a ResourcePatternResolver, which can be checked through
instanceof ResourcePatternResolver. See also theResourcePatternUtils.getResourcePatternResolvermethod.Invoked after population of normal bean properties but before an init callback like InitializingBean's
afterPropertiesSetor a custom init-method. Invoked before ApplicationContextAware'ssetApplicationContext.- Specified by:
setResourceLoaderin interfaceResourceLoaderAware- Parameters:
resourceLoader- the ResourceLoader object to be used by this object- See Also:
ResourcePatternResolver,ResourcePatternUtils.getResourcePatternResolver(org.springframework.core.io.ResourceLoader)
getOrder
public int getOrder()
Description copied from interface:OrderedGet 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-startupvalues).Same order values will result in arbitrary sort positions for the affected objects.
- Specified by:
getOrderin interfaceOrdered- Returns:
- the order value
- See Also:
Ordered.HIGHEST_PRECEDENCE,Ordered.LOWEST_PRECEDENCE
predictBeanType
public Class<?> predictBeanType(Class<?> beanClass, String beanName)
Description copied from interface:SmartInstantiationAwareBeanPostProcessorPredict the type of the bean to be eventually returned from this processor'sInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(java.lang.Class<?>, java.lang.String)callback.- Specified by:
predictBeanTypein interfaceSmartInstantiationAwareBeanPostProcessor- Overrides:
predictBeanTypein classInstantiationAwareBeanPostProcessorAdapter- Parameters:
beanClass- the raw class of the beanbeanName- the name of the bean- Returns:
- the type of the bean, or
nullif not predictable
postProcessBeforeInstantiation
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName)
Description copied from interface:InstantiationAwareBeanPostProcessorApply 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
BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String)callback from the configuredBeanPostProcessors.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
SmartInstantiationAwareBeanPostProcessorinterface in order to predict the type of the bean object that they are going to return here.- Specified by:
postProcessBeforeInstantiationin interfaceInstantiationAwareBeanPostProcessor- Overrides:
postProcessBeforeInstantiationin classInstantiationAwareBeanPostProcessorAdapter- Parameters:
beanClass- the class of the bean to be instantiatedbeanName- the name of the bean- Returns:
- the bean object to expose instead of a default instance of the target bean, or
nullto proceed with default instantiation - See Also:
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation(java.lang.Object, java.lang.String),AbstractBeanDefinition.getBeanClass(),AbstractBeanDefinition.getFactoryMethodName()
prepareScriptBeans
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 BeanFactoryscriptFactoryBeanName- the name of the internal ScriptFactory beanscriptedObjectBeanName- the name of the internal scripted object bean
resolveRefreshCheckDelay
protected long resolveRefreshCheckDelay(BeanDefinition beanDefinition)
Get the refresh check delay for the givenScriptFactoryBeanDefinition. If theBeanDefinitionhas ametadata attributeunder the keyREFRESH_CHECK_DELAY_ATTRIBUTEwhich is a validNumbertype, then this value is used. Otherwise, thedefaultRefreshCheckDelayvalue is used.- Parameters:
beanDefinition- the BeanDefinition to check- Returns:
- the refresh check delay
resolveProxyTargetClass
protected boolean resolveProxyTargetClass(BeanDefinition beanDefinition)
createScriptFactoryBeanDefinition
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:
ScriptFactory
getScriptSource
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 beanscriptSourceLocator- the script source locator associated with the bean- Returns:
- the corresponding ScriptSource instance
- See Also:
convertToScriptSource(java.lang.String, java.lang.String, org.springframework.core.io.ResourceLoader)
convertToScriptSource
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 beanscriptSourceLocator- the script source locatorresourceLoader- the ResourceLoader to use (if necessary)- Returns:
- the ScriptSource instance
createConfigInterface
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 forinterfaces- the interfaces to check against (might define getters corresponding to the setters we're supposed to generate)- Returns:
- the config interface
- See Also:
InterfaceMaker,BeanUtils.findPropertyType(java.lang.String, java.lang.Class<?>...)
createCompositeInterface
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
- See Also:
Proxy.getProxyClass(java.lang.ClassLoader, java.lang.Class<?>...)
createScriptedObjectBeanDefinition
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 definitionscriptFactoryBeanName- the name of the internal ScriptFactory beanscriptSource- the ScriptSource for the scripted beaninterfaces- the interfaces that the scripted bean is supposed to implement- Returns:
- the extracted ScriptFactory bean definition
- See Also:
ScriptFactory.getScriptedObject(org.springframework.scripting.ScriptSource, java.lang.Class<?>...)
createRefreshableProxy
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass)
Create a refreshable proxy for the given AOP TargetSource.- Parameters:
ts- the refreshable TargetSourceinterfaces- the proxy interfaces (may benullto indicate proxying of all interfaces implemented by the target class)- Returns:
- the generated proxy
- See Also:
RefreshableScriptTargetSource
destroy
public void destroy()
Destroy the inner bean factory (used for scripts) on shutdown.- Specified by:
destroyin interfaceDisposableBean