|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter org.springframework.scripting.support.ScriptFactoryPostProcessor
public class ScriptFactoryPostProcessor
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 followin 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>
Field Summary | |
---|---|
static String |
INLINE_SCRIPT_PREFIX
The Resource -style prefix that denotes
an inline script. |
protected Log |
logger
Logger available to subclasses |
static String |
REFRESH_CHECK_DELAY_ATTRIBUTE
|
Constructor Summary | |
---|---|
ScriptFactoryPostProcessor()
|
Method Summary | |
---|---|
protected ScriptSource |
convertToScriptSource(String scriptSourceLocator,
ResourceLoader resourceLoader)
Convert the given script source locator to a ScriptSource instance. |
protected Class |
createConfigInterface(PropertyValue[] pvs,
Class[] interfaces)
Create a config interface for the given bean property values. |
protected Object |
createRefreshableProxy(TargetSource ts,
Class[] interfaces)
Create a refreshable proxy for the given AOP TargetSource. |
protected RootBeanDefinition |
createScriptedObjectBeanDefinition(RootBeanDefinition 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 RootBeanDefinition |
createScriptFactoryBeanDefinition(RootBeanDefinition 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). |
void |
destroy()
Destroy the inner bean factory (used for scripts) on shutdown. |
Object |
postProcessBeforeInstantiation(Class beanClass,
String beanName)
Apply this BeanPostProcessor before the target bean gets instantiated. |
protected long |
resolveRefreshCheckDelay(BeanDefinition bd)
Get the refresh check delay for the given ScriptFactory BeanDefinition . |
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. |
Methods inherited from class org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter |
---|
postProcessAfterInitialization, postProcessAfterInstantiation, postProcessBeforeInitialization, postProcessPropertyValues |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String INLINE_SCRIPT_PREFIX
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.
public static final String REFRESH_CHECK_DELAY_ATTRIBUTE
protected final Log logger
Constructor Detail |
---|
public ScriptFactoryPostProcessor()
Method Detail |
---|
public void setDefaultRefreshCheckDelay(long defaultRefreshCheckDelay)
Note that an actual refresh will only happen when
the ScriptSource
indicates
that it has been modified.
ScriptSource.isModified()
public void setBeanFactory(BeanFactory beanFactory)
BeanFactoryAware
Invoked after the population of normal bean properties but
before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (may not be null
).
The bean can immediately call methods on the factory.BeanInitializationException
public void setResourceLoader(ResourceLoader resourceLoader)
ResourceLoaderAware
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
.
setResourceLoader
in interface ResourceLoaderAware
resourceLoader
- ResourceLoader object to be used by this objectResourcePatternResolver
,
ResourcePatternUtils.getResourcePatternResolver(org.springframework.core.io.ResourceLoader)
public Object postProcessBeforeInstantiation(Class beanClass, String beanName)
InstantiationAwareBeanPostProcessor
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 configured
BeanPostProcessors
.
This callback will only be applied to bean definitions with a bean class. In particular, it will not be applied to beans with a "factory-method".
postProcessBeforeInstantiation
in interface InstantiationAwareBeanPostProcessor
postProcessBeforeInstantiation
in class InstantiationAwareBeanPostProcessorAdapter
beanClass
- the class of the bean to be instantiatedbeanName
- the name of the bean
null
to proceed with default instantiationAbstractBeanDefinition.hasBeanClass()
,
AbstractBeanDefinition.getFactoryMethodName()
protected long resolveRefreshCheckDelay(BeanDefinition bd)
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.
protected RootBeanDefinition createScriptFactoryBeanDefinition(RootBeanDefinition bd)
bd
- the full script bean definition
ScriptFactory
protected ScriptSource convertToScriptSource(String scriptSourceLocator, ResourceLoader resourceLoader)
By default, supported locators are Spring resource locations (such as "file:C:/myScript.bsh" or "classpath:myPackage/myScript.bsh") and inline scripts ("inline:myScriptText...").
scriptSourceLocator
- the script source locatorresourceLoader
- the ResourceLoader to use (if necessary)
protected Class createConfigInterface(PropertyValue[] pvs, Class[] interfaces)
This implementation creates the interface via CGLIB's InterfaceMaker, determining the property types from the given interfaces (as far as possible).
pvs
- the bean property values to create a config interface forinterfaces
- the interfaces to check against (might define
getters corresponding to the setters we're supposed to generate)
InterfaceMaker
,
BeanUtils.findPropertyType(java.lang.String, java.lang.Class[])
protected RootBeanDefinition createScriptedObjectBeanDefinition(RootBeanDefinition bd, String scriptFactoryBeanName, ScriptSource scriptSource, Class[] interfaces)
bd
- the full script bean definition
ScriptFactory.getScriptedObject(org.springframework.scripting.ScriptSource, java.lang.Class[])
protected Object createRefreshableProxy(TargetSource ts, Class[] interfaces)
ts
- the refreshable TargetSourceinterfaces
- the proxy interfaces (may be null
to
indicate proxying of all interfaces implemented by the target class)
RefreshableScriptTargetSource
public void destroy()
destroy
in interface DisposableBean
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |