I would like to use the Spring Framework Dynamic Language Support to create a rebootable w810> ( at run time! ) From the Groovy script. I want to avoid the xml configuration and use annotations (or the like) in the context of the Spring Boot application.
This is the extension for the question that has already been asked , the extension that I want my hands to be dirty with BeanPostProcessors , Handlers , Parsers , whatever it takes .
I quickly looked through the javadoc for ScriptFactoryPostProcessor and came up with working examples. I want to know why Application.groovy (v2) not working?
beans.xml - it works! (but I want to define beans in Application.groovy instead of xml ...)
<bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"> <property name="defaultRefreshCheckDelay" value="1000" /> </bean> <bean id="foobar0" class="org.springframework.scripting.groovy.GroovyScriptFactory"> <constructor-arg value="file:/C:/someDir/src/main/static/FoobarService.groovy"/> </bean>
Application.groovy (v1) - works! (but a very ugly workaround)
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application) // Add GroovyScriptFactory after Application is prepared... app.addListeners(new ApplicationListener<ApplicationPreparedEvent>() { void onApplicationEvent(ApplicationPreparedEvent event) { def registry = (BeanDefinitionRegistry) event.applicationContext.autowireCapableBeanFactory def bd = BeanDefinitionBuilder.genericBeanDefinition(GroovyScriptFactory) .addConstructorArgValue("file:/C:/someDir/src/main/static/FoobarService.groovy") .getBeanDefinition() bd.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, 1000) registry.registerBeanDefinition('foobar0', bd) } }) app.run(args) } @Bean ScriptFactoryPostProcessor scriptFactory() { new ScriptFactoryPostProcessor() } }
Application.groovy (v2) - doesn't work - why not?
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application, args) } @Bean ScriptFactoryPostProcessor scriptFactory() { new ScriptFactoryPostProcessor() } @Bean GroovyScriptFactory foobar0() { new GroovyScriptFactory("file:/C:/someDir/src/main/static/FoobarService.groovy") } }
This seems to be related to the way / when the beans definitions are initialized in the ApplicationContext life cycle. I tried using @Order and @DependsOn to manage the bean order - to no avail. It is worth mentioning that now I am repeating the following log: it seems that ScriptFactoryPostProcessor constantly rewriting the bean using the definition of "null" bean (why?).
2015-08-27 12:04:11.312 INFO 5780 --- [ main] osbfsDefaultListableBeanFactory : Overriding bean definition for bean 'scriptFactory.foobar0': replacing [Generic bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; p rimary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=n ull] with [Generic bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=0; depen dencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; i nitMethodName=null; destroyMethodName=null]
Connected:
- SPR-10253 - Updating Groovy annotated controllers throws a ClassCastException
- SPR-10689 - tag in version 2.5 and later does not work for Spring MVC updatable endpoints
- SPR-12300 - Add support for dynamic languages ββbeing updated by beans in the @Configuration class
java spring spring-annotations spring-boot groovy
Nick grealy
source share