我正在尝试更深入地了解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-template,但是没有生成web.xml ...我还查看了默认生成的插件描述符,它似乎没有doWithWebDescriptor……
我想知道这是否已经改变了-不再生成web.xml,或者是否应该在grails 3下注册一个侦听器。
发布于 2015-05-05 19:02:47
我已经设法让默认的tomcat websocket侦听器通过一个spring boot grails应用程序工作:
文档如下所示:
https://github.com/vahidhedayati/testwebsocket-grails3
我已经决定更新这篇文章,并包括我到目前为止在这个问题上的所有发现。
更具体地说,是应用程序grails- application.groovy /init文件夹中的应用程序:
此bean启动默认的tomcat websocket侦听器:
@Bean
public ServletListenerRegistrationBean<AnotherWebSocketHandler> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<AnotherWebSocketHandler>(new AnotherWebSocketHandler());
}在尝试在插件中重用时,其结果是:
上面的项目是一个基本的grails应用程序,它做两件事,一个基本的spring套接字以及java1.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方法来启用其他侦听器……我本可以在所有的通话中使用最高一级的。留作日后参考之用。
有了这一点,spring boot现在可以像注册侦听器时一样模拟web.xml。从那里加载websocket的实际groovy类仍然是原来的样子,即使用默认的websocket调用,如onOpen、onMessage等。
发布于 2021-08-09 19:32:44
从Grails3开始,在插件方法doWithSpring中使用spring注册bean来添加运行时配置。不再使用doWithWebDescriptor。
这应该适用于Servlet Listeners
Closure doWithSpring() {{ ->
MyListener(ServletListenerRegistrationBean) {
listener = bean(someClass)
order = Ordered.HIGHEST_PRECEDENCE
}
}}免责声明:我没有测试这段代码。
https://stackoverflow.com/questions/28902928
复制相似问题