私はgrailsプロジェクトのBootstrap.groovyにあるdestroy destroyのBeanにアクセスしようとしています。どのようにこれを達成するための任意のアイデア?Grails:ブートストラップ・コードのクローズにSpring Beanにアクセスするには?
私はservletContextにアクセスできないようです...?
私はgrailsプロジェクトのBootstrap.groovyにあるdestroy destroyのBeanにアクセスしようとしています。どのようにこれを達成するための任意のアイデア?Grails:ブートストラップ・コードのクローズにSpring Beanにアクセスするには?
私はservletContextにアクセスできないようです...?
あなたは、コードのチャンクを使用して(ブートストラップの破壊閉鎖を含む)どこからでものApplicationContextへの参照を取得することができます:Beanへの参照を取得する
def ctx = org.codehaus.groovy.grails.web.context.ServletContextHolder.servletContext.getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
はctx.beanName
と同じくらい簡単です。ここで
は、この作業を簡素化することができます(Javaで書かれた)小さなutilのクラスです:
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")
乾杯、SIGI
まあ、の例もありません。ブートストラップの破壊ブロックを使用しています。ドキュメントから:推測として
It is not guaranteed that {{destroy}} will be called unless the application exits gracefully (for example by using the application server's shutdown command) so don't rely on it too much
、私は、ブートストラップの{{}}破壊クロージャが実行される前に豆アクセスしようとしているようなServletContextが既に破壊されていることを言わなければならないと思いますすでに消えてしまった。誰でも確認できますか?
私は」知っています私はすべてGoogleでこれを見つけて以来、すべて遅れています...
あなたのBootStrapクラスは私になりますすべてのサービスやコントローラやものと同じように、Spring Beanで名前を付けられません。 Beanが必要な場合は、名前で定義してください。表示されます。それ以外の場合は、grailsApplicationをデフオルトしてgrailsApplication.mainContext.getBeanなどに行きます。