You can get a link to applicationContext everywhere (including destroying BootStrap closure) using this piece of code:
def ctx = org.codehaus.groovy.grails.web.context.ServletContextHolder.servletContext.getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
Getting a bean reference is as simple as ctx.beanName .
Here is a small util class (written in Java) that can simplify this task:
import org.springframework.context.ApplicationContext; import org.codehaus.groovy.grails.web.context.ServletContextHolder; import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes; public class SpringUtil { public static ApplicationContext getCtx() { return getApplicationContext(); } public static ApplicationContext getApplicationContext() { return (ApplicationContext) ServletContextHolder.getServletContext().getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT); } @SuppressWarnings("unchecked") public static <T> T getBean(String beanName) { return (T) getApplicationContext().getBean(beanName); } }
and example:
def bean = SpringUtil.getBean("beanName")
Cheers, Sigi
Siegfried puchbauer
source share