我正在尝试进一步了解 grails3,但我不确定插件描述符和 doWithWebDescriptor:
src/main/groovy/grails/plugin/plugin/PluginGrailsPlugin.groovy
def doWithWebDescriptor = { xml ->
def listenerNode = xml.'listener'
listenerNode[listenerNode.size() - 1] + {
listener {
'listener-class'(someClass.name)
}
}
}
我在 grails 3 下尝试了 grails install-templates 并且没有生成 web.xml ......我还查看了默认生成的插件描述符,它似乎没有 doWithWebDescriptor ......
想知道这是否已经改变 - 它不再产生 web.xml 或者我应该做什么来在 grails 3 下注册一个监听器。
最佳答案
我已经设法让默认的tomcat websocket监听器通过spring boot grails应用程序工作:
它记录在这里:
https://github.com/vahidhedayati/testwebsocket-grails3
我决定更新这篇文章,并包括到目前为止我在这个问题上的所有发现。
更具体地说 application.groovy在您的应用程序 grails-app/init 文件夹中:
这个 bean 启动默认的 tomcat websocket 监听器:
@Bean
public ServletListenerRegistrationBean<AnotherWebSocketHandler> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<AnotherWebSocketHandler>(new AnotherWebSocketHandler());
}
在搞乱插件重用的同时,发现是:
上面的项目是一个基本的 grails 应用程序,它做了两件事,一个基本的 spring socket 以及 java 1.X Websocket:
Here is how to use Default websocket in a grails 3 plugin
在你plugin descriptor你有这样的事情:
Closure doWithSpring() {
{->
wsChatConfig DefaultWsChatConfig
}
}
在这个插件中,我留下了两种启动监听器的方法:
@Bean
public ServletContextInitializer myInitializer() {
return new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(WsCamEndpoint)
servletContext.addListener(WsChatFileEndpoint)
}
}
}
// Alternative way
@Bean
public ServletListenerRegistrationBean<WsChatEndpoint> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<WsChatEndpoint>(new WsChatEndpoint())
}
top 方法非常方便,因为您只能初始化 1 个 ServletListenerRegistrationBean,而我不得不求助于 top 方法来启用其他监听器……我本可以将 top primary 用于所有调用。留作日后引用。。
有了这个,spring boot 现在可以模拟 web.xml 在注册监听器时的模拟。从那里加载 websocket 的实际 groovy 类保持原样,即使用默认的 websocket 调用,例如 onOpen onMessage 等。
关于Grails:Grails3:doWithWebDescriptor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28902928/