Hi Tom
Short answer: use an ApplicationContext implementation, and not a simple BeanFactory. To wit:
Code:
package man;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Groman {
public static void main(String[] args) {
// ClassPathResource res = new ClassPathResource("beans.xml");
// XmlBeanFactory factory = new XmlBeanFactory(res);
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Object obj = ctx.getBean("messenger");
System.out.println(obj);
// prints something like 'org.springframework.scripting.groovy.GroovyMessenger@d3c65d'
}
}
(You will also need to have the following libraries on your project classpath in addition to Spring, Commons Logging, and Groovy: antlr-2.7.6.jar and asm-2.2.2.jar. I have already added sidebars to the dynamic languages chapter documenting the libraries required for each supported dynamic language.)
Long(ish) answer: use an ApplicationContext implementation, and not a simple BeanFactory, because the scripting support utilises a BeanPostProcessor implementation (ScriptFactoryPostProcessor) to transparently replace dynamic language bean definitions with AOP proxies so that everything works correctly.
The reference documentation is not explicit about this, so I will go back and make some (small) changes in light of your post. Thanks for spotting this prior to the final release.
Also, you will really also want to have your Groovy class declaration implement an interface. In the case of your example, be sure to implement the Messenger interface like so:
Code:
package org.springframework.scripting.groovy;
class GroovyMessenger implements Messenger {
@Property String message;
}
Cheers
Rick