简体   繁体   中英

Setting parent application context in a grails application

I have a Spring application and I want to run in it an embedded Jetty instance where I want to deploy a Grails application.

I need the Grails application to have access to the application context of the Spring application.

I am deploying the Grails application as following:

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

To get the Grails application access to Spring application context I further added these lines before calling webServer.start() :

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

ParentAwareContextLoaderListener is a simple class like this:

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;
}
}

I further removed the original ContextLoaderListener from the Grails application web.xml. While this approach worked for one other non grails application, it is for some reason not working with Grails. I get this exception:

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)

Any idea how can this problem be solved? Thank you for any help

I finally used a completely different approach. See for example this link http://techo-ecco.com/blog/spring-application-context-hierarchy-and-contextsingletonbeanfactorylocator/ for solution which uses ContextSingletonBeanFactoryLocator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM