Grails:在Bootstrap代码的关闭中访问spring beans? [英] Grails: Accessing spring beans in the destory closure of Bootstrap code?
问题描述
我正在寻找在我的grails项目的Bootstrap.groovy中的destroy关闭中访问一个bean。
我似乎无法访问servletContext ...?
您可以使用该块代码从各处获得对applicationContext的引用(包括BootStrap的销毁关闭):
def ctx = org.codehaus.groovy.grails.web.context.ServletContextHolder.servletContext.getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
获取对bean的引用就像 这是一个小的util类(用Java编写),可以简化这个任务: 以及一个例子: 干杯,Sigi I'm looking to access a bean in my destroy closure in the Bootstrap.groovy of my grails project. Any ideas on how to achieve this? I seem to have no access to servletContext...? You can obtain a a reference to the applicationContext from everywhere (including the destroy closure of BootStrap) using that chunk of code: Getting a reference to a bean is as easy as Here is a small util class (written in Java) that can simplify this task: and an example: Cheers, Sigi 这篇关于Grails:在Bootstrap代码的关闭中访问spring beans?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ctx.beanName <
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);
}
}
def bean = SpringUtil.getBean(beanName)
def ctx = org.codehaus.groovy.grails.web.context.ServletContextHolder.servletContext.getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
ctx.beanName
.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);
}
}
def bean = SpringUtil.getBean("beanName")