org.springframework.scripting.support

Class ScriptFactoryPostProcessor

  • All Implemented Interfaces:
    Aware, BeanClassLoaderAware, BeanFactoryAware, BeanPostProcessor, InstantiationAwareBeanPostProcessor, SmartInstantiationAwareBeanPostProcessor, DisposableBean, ResourceLoaderAware, Ordered


    public class ScriptFactoryPostProcessor
    extends InstantiationAwareBeanPostProcessorAdapter
    implements BeanClassLoaderAware, BeanFactoryAware, ResourceLoaderAware, DisposableBean, Ordered
    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.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>
    Since:
    2.0
    Author:
    Juergen Hoeller, Rob Harrop, Rick Evans, Mark Fisher