У меня есть приложение 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;
}
}
Далее я удалил оригинальный ContextLoaderListener из файла web.xml приложения Grails. Хотя этот подход работал для одного другого приложения, отличного от 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)
Любая идея, как можно решить эту проблему? Спасибо за любую помощь