I\'d like to use Dynamic Languages Support of Spring Framework.
In XML I\'d just use the lang namespace, but I\'d like to use Java configuration (i.e. <
I'm going down the same path (work in progress), and have managed to initialise reloadable Groovy scripts by adding the bean definitions when the Spring Application is prepared. In my example I'm using spring-boot.
If you add the following AddBeanDefinitionsListener listener class and a ScriptFactoryPostProcessor bean, you can initialise Groovy scripts with very little effort...
AddBeanDefinitionsListener.groovy
public class AddBeanDefinitionsListener
implements ApplicationListener {
Map beanDefs
AddBeanDefinitionsListener(Map beanDefs) {
this.beanDefs = beanDefs
}
@Override
void onApplicationEvent(ApplicationPreparedEvent event) {
def registry = (BeanDefinitionRegistry) event.applicationContext
.autowireCapableBeanFactory
beanDefs.each { String beanName, BeanDefinition beanDef ->
registry.registerBeanDefinition(beanName, beanDef)
}
}
/* Static Utility methods */
static BeanDefinition groovyScriptFactory(String scriptLocation) {
def bd = BeanDefinitionBuilder.genericBeanDefinition(GroovyScriptFactory)
.addConstructorArgValue(scriptLocation)
.getBeanDefinition()
bd.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, 1000)
bd
}
}
Application.groovy
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application)
app.addListeners(new AddBeanDefinitionsListener([
'foobar0': groovyScriptFactory("file:/some/path/Foobar0Service.groovy"),
'foobar1': groovyScriptFactory("file:/some/path/Foobar1Service.groovy")
]))
app.run(args)
}
@Bean
ScriptFactoryPostProcessor scriptFactory() {
new ScriptFactoryPostProcessor()
}
}
(Might be nicer if implemented with Spring 4.2's events?)