我有一个Spring应用程序,我想在其中运行一个嵌入式Jetty实例,我想在其中部署Grails应用程序。
我需要Grails应用程序才能访问Spring应用程序的应用程序上下文。
我正在部署Grails应用程序,如下所示:
Server webServer = new Server(8080);
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar("MyGrailsApp.war");
webServer.setHandler(webapp);
webServer.start();
要让Grails应用程序访问Spring应用程序上下文,我在调用 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 :(得分:0)
我终于使用了一种完全不同的方法。有关使用ContextSingletonBeanFactoryLocator的解决方案,请参阅此链接http://techo-ecco.com/blog/spring-application-context-hierarchy-and-contextsingletonbeanfactorylocator/。