2012-10-11 65 views
1

我有一個Spring應用程序,我想在其中運行一個嵌入式Jetty實例,我想部署一個Grails應用程序。在grails應用程序中設置父應用程序上下文

我需要Grails應用程序訪問Spring應用程序的應用程序上下文。

我部署Grails應用程序如下:

Server webServer = new Server(8080); 
    WebAppContext webapp = new WebAppContext(); 
    webapp.setContextPath("/"); 
    webapp.setWar("MyGrailsApp.war"); 
    webServer.setHandler(webapp); 
    webServer.start(); 

要到Spring應用程序上下文的Grails應用程序訪問我打電話webServer.start()前進一步補充這些行:

ParentAwareContextLoaderListener contextLoaderListener = new ParentAwareContextLoaderListener(); 
    //appContext is context of my Spring application 
    contextLoaderListener.setApplicationContext(appContext); 
    webapp.addEventListener(contextLoaderListener); 

ParentAwareContextLoaderListener是這樣一個簡單的類:

public class ParentAwareContextLoaderListener extends ContextLoaderListener implements ApplicationContextAware { 
private ApplicationContext applicationContext; 


@Override 
protected ApplicationContext loadParentContext(final ServletContext servletContext) { 
    return applicationContext; 
} 

@Override 
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException { 
    this.applicationContext = applicationContext; 
} 
} 

我進一步從Grails應用程序web.xml中刪除了原始的ContextLoaderListener。雖然這種方法適用於其他非Grails應用程序,但出於某種原因,不適用於Grails。我得到這個例外:

20138 [main] ERROR org.springframework.web.context.ContextLoader ContextLoader - Context initialization failed 
java.lang.IllegalArgumentException: ConfigurableWebApplicationContext environment must be of type ConfigurableWebEnvironmentObject of class [org.springframework.core.env.StandardEnvironment] must be an instance of interface org.springframework.web.context.ConfigurableWebEnvironment 
    at org.springframework.util.Assert.isInstanceOf(Assert.java:337) 
    at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.getEnvironment(AbstractRefreshableWebApplicationContext.java:147) 
    at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.getEnvironment(AbstractRefreshableWebApplicationContext.java:1) 
    at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.resolvePath(AbstractRefreshableConfigApplicationContext.java:122) 
    at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.setConfigLocations(AbstractRefreshableConfigApplicationContext.java:81) 
    at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.setConfigLocation(AbstractRefreshableConfigApplicationContext.java:69) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:380) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) 

任何想法如何解決這個問題?謝謝你的幫助

+0

我面臨同樣的問題。檢查所有的彈簧組件,spring-framework,spring-mvc,例如確保版本是合併的 –

回答

相關問題