[SPRING] Grails : 부트 스트랩 코드가 완전히 닫힙니다.
SPRINGGrails : 부트 스트랩 코드가 완전히 닫힙니다.
내 grails 프로젝트의 Bootstrap.groovy에서 destroy closure에있는 bean에 접근하려고한다. 이것을 달성하는 방법에 대한 아이디어?
servletContext에 대한 액세스 권한이없는 것 같습니다 ...?
해결법
-
==============================
1.다음과 같은 코드를 사용하여 어디서나 applicationContext에 대한 참조를 얻을 수 있습니다 (BootStrap의 destroy closure 포함).
다음과 같은 코드를 사용하여 어디서나 applicationContext에 대한 참조를 얻을 수 있습니다 (BootStrap의 destroy closure 포함).
def ctx = org.codehaus.groovy.grails.web.context.ServletContextHolder.servletContext.getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
bean에 대한 참조를 얻는 것은 ctx.beanName만큼 쉽습니다.
다음은이 태스크를 단순화 할 수있는 작은 유틸리티 클래스 (Java로 작성)입니다.
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")
건배, 처지기
-
==============================
2.나는 내가이 모든 것을 Google을 통해 발견 한 이래로 모두 늦었다 고 알고있다.
나는 내가이 모든 것을 Google을 통해 발견 한 이래로 모두 늦었다 고 알고있다.
BootStrap 클래스는 모든 서비스와 컨트롤러와 마찬가지로 Spring bean을 이름으로 주입한다. 빈을 원한다면 이름으로 정의하면 나타납니다. 그렇지 않으면, grailsApplication을 정의하고 grailsApplication.mainContext.getBean 등으로 이동하십시오.
-
==============================
3.흠, 부트 스트랩에서 블록 파괴를 사용하는 사람조차도 어떤 사례도 찾을 수 없습니다. 문서에서 :
흠, 부트 스트랩에서 블록 파괴를 사용하는 사람조차도 어떤 사례도 찾을 수 없습니다. 문서에서 :
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
짐작할 수 있듯이 부트 스트랩의 {{destroy}} 클로저가 실행되기 전에 servletContext가 이미 파괴되었으므로 액세스하려는 bean이 이미 없어 졌다고해야합니다. 누구든지 확인할 수 있습니까?
from https://stackoverflow.com/questions/574807/grails-accessing-spring-beans-in-the-destory-closure-of-bootstrap-code by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] 스프링 의존성 주석이 달린 애스펙트 삽입하기 (0) | 2019.02.11 |
---|---|
[SPRING] 스프링 보안 제거 RoleVoter 접두사 (0) | 2019.02.11 |
[SPRING] 하나의 객체에서 DTO와 Entity? (0) | 2019.02.11 |
[SPRING] Spring @ Transactional Annotation : 자체 호출 (0) | 2019.02.11 |
[SPRING] ClassCastException $ 프록시는 aop을 사용하여로 캐스트 될 수 없습니다. (0) | 2019.02.11 |