Using Scripting Languages with BeanFactory

As of version 1.1 of Spring, the BeanFactory implementations could only create beans from already compiled Java classes. For the most part, this is what you want, but in some cases, you might find it useful to define a bean as a script and have this script compiled automatically. With the new scripting support features that are in development, this will soon be possible.

The main goal for this functionality is that you are able to define Web Tier controllers using a scripting language and enable quick and easy modification of these controller scripts. This makes sense for most, if not all, web applications where controllers are essentially wrappers that invoke logic in the Business Tier. Using a scripting language makes developing and modifying Web Tier controllers much easier.

The typical usage pattern for the scripting functionality is to define an interface or base class in Java or to use preexisting interfaces or classes such as Controller or AbstractController, then implement this interface in a scripting language, and then have Spring compile the script on the fly for you. For example, consider the interface shown in Listing D-1.

Listing D-1: The PersonBean Interface

image from book
package com.apress.prospring.apd.groovy;      public interface PersonBean {          String getName();     int getAge(); }
image from book

Here you can see a simple interface that defines two methods: getName() and getAge(). Now consider the code shown in Listing D-2 that loads two instances of PersonBean from the BeanFactory and displays the value returned by getName().

Listing D-2: Using the PersonBean Interface

image from book
package com.apress.prospring.apd.groovy;      import org.springframework.context.ApplicationContext; import;      public class Example {          public static void main(String[] args) {         ApplicationContext ctx = new FileSystemXmlApplicationContext(                 "./ap4/src/conf/groovy/groovy.xml");              PersonBean robh = (PersonBean) ctx.getBean("robh");         System.out.println("Name: " + robh.getName());                  PersonBean janm = (PersonBean) ctx.getBean("janm");         System.out.println("Name: " + janm.getName());     } }
image from book

Here you can see that we are loading two beans of type PersonBean from the ApplicationContext. As of yet, we have not created an implementation of PersonBean in Java and we are not going to. Instead, we create two implementations in Groovy and have Spring compile them on the fly. If you are unfamiliar with Groovy, visit Listing D-3 shows the Groovy class for the robh bean and Listing D-4 shows the class for the janm bean.

Listing D-3: Groovy Class for robh Bean

image from book
package com.apress.prospring.apd.groovy;      class RobHarrop implements PersonBean {          String getName() {         return "Rob Harrop";        }                int getAge() {            return 100;        } } 
image from book

Listing D-4: Groovy Class for janm Bean

image from book
package com.apress.prospring.apd.groovy;      class JanMachacek implements PersonBean {          String getName() {         return "Jan Machacek";        }                int getAge() {            return 100;        } }
image from book

To have Spring compile these scripts into beans automatically, we use the factory-bean and factory-method attributes of the bean tag in conjunction with the GroovyScriptFactory class, as shown in Listing D-5.

Listing D-5: Compiling Groovy Scripts into Beans

image from book
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  ""> <beans>     <bean         />     <bean  factory-bean="groovyScriptFactory" factory-method="create">         <constructor-arg index="0">             <value>./ap4/src/conf/groovy/robh.groovy</value>         </constructor-arg>     </bean>     <bean  factory-bean="groovyScriptFactory" factory-method="create">         <constructor-arg index="0">             <value>./ap4/src/conf/groovy/janm.groovy</value>         </constructor-arg>     </bean> </beans>
image from book

Here we use the factory-bean attribute of the bean tag to inform Spring that the groovyScriptFactory bean, which is of type GroovyScriptFactory, should be used to create the robh and janm beans. In addition, we use the factory-method attribute to instruct Spring to use the create() method of the groovyScriptFactory bean when it creates the beans and the <constructor-arg> tag to pass arguments to this method.

If you run this example, after a short delay, you see the following output:

Name: Rob Harrop Name: Jan Machacek 

As you can see, Spring compiled the scripts shown in Listings D-3 and D-4 and used the resulting classes to create the beans. As mentioned, you do experience a short delay when you start this application—a result of the script compilation process.

In its current incarnation, BeanFactory script integration has support for both Groovy and BeanShell scripts. Currently, script support is scheduled for version 1.2 of Spring; however, the development team is discussing bringing the 1.2 release forward so that features such as JMX and JSR-175 support can be released earlier. As a result, script support might not appear until Spring 1.3.

Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: