servlet和过滤器在Grails中注册为Springbeans而不是web.xml

servlet和过滤器在Grails中注册为Springbeans而不是web.xml,spring,grails,Spring,Grails,在Grails中,人们注意到“新的servlet和过滤器可以分别注册为Springbean或ServletRegistrationBean和FilterRegistrationBean”,但是关于这一点没有太多其他的说法 我想知道是否有人对如何正确地做到这一点有什么好的建议(例如,使用init/BootStrap.groovy,它包含servlet上下文来加载bean,而不是conf/spring中的bean),或者可能有一些预定义的spring方法来做到这一点,这是显而易见的,但我没有 注意:

在Grails中,人们注意到“新的servlet和过滤器可以分别注册为Springbean或ServletRegistrationBean和FilterRegistrationBean”,但是关于这一点没有太多其他的说法

我想知道是否有人对如何正确地做到这一点有什么好的建议(例如,使用init/BootStrap.groovy,它包含servlet上下文来加载bean,而不是conf/spring中的bean),或者可能有一些预定义的spring方法来做到这一点,这是显而易见的,但我没有


注意:我正在尝试将SpringWS集成到Grails3.0.6中。

对于插件,您应该在
doWithSpring
中进行集成,对于应用,您应该在
GrailsApp/conf/spring/resources.groovy
中进行集成。由于Grails3是基于SpringBoot的,所以还可以使用
@Bean
方法

当应用程序上下文启动时,Spring会查找
ServletRegistrationBean
s、
FilterRegistrationBean
s等,并使用它们配置的属性在servlet容器中为您进行编程注册

Grails源代码中有一些示例。ControllersGrailsPlugin注册一些过滤器(例如),并注册主DispatcherServlet

中有一些文档,尽管它偏向于
@Bean
方法,但是您可以使用任何方法来定义Bean。

(适用于Grails 3(特别是3.3.3版))

要使用插件描述符文件(*GrailsPlugin.groovy)的“doWithSpring”部分将自定义侦听器添加到servletContext,请执行以下操作:

步骤1

*GrailsPlugin.groovy

...
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean
import my.custom.listeners.ContextListener
...
class MyAppGrailsPlugin extends Plugin {
...
    Closure doWithSpring() {
        {->
        ...
        httpSessionServletListener(ServletListenerRegistrationBean){
            listener = bean(ContextListener)
        }
        ...
    }
...
}
class SomeService{        
    ...
    def httpSessionServletListener
    ...
    def someMethod(){
        httpSessionServletListener.getSessions()
    }
    ...
}
import javax.servlet.http.HttpSession
import javax.servlet.http.HttpSessionListener

public class ContextListener implements HttpSessionListener {
    ...
    /**
     * All current sessions.
     */
    public List<HttpSession> getSessions() {
        synchronized (this) {
            return new ArrayList<HttpSession>(sessions)
        }
    }
    ...
}
步骤#2:现在它可以在服务类中称为:

SomeService.groovy

...
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean
import my.custom.listeners.ContextListener
...
class MyAppGrailsPlugin extends Plugin {
...
    Closure doWithSpring() {
        {->
        ...
        httpSessionServletListener(ServletListenerRegistrationBean){
            listener = bean(ContextListener)
        }
        ...
    }
...
}
class SomeService{        
    ...
    def httpSessionServletListener
    ...
    def someMethod(){
        httpSessionServletListener.getSessions()
    }
    ...
}
import javax.servlet.http.HttpSession
import javax.servlet.http.HttpSessionListener

public class ContextListener implements HttpSessionListener {
    ...
    /**
     * All current sessions.
     */
    public List<HttpSession> getSessions() {
        synchronized (this) {
            return new ArrayList<HttpSession>(sessions)
        }
    }
    ...
}
步骤#0:应编写自定义筛选器类

下面是实现各自接口的自定义侦听器类的代码段:

ContextListener.groovy

...
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean
import my.custom.listeners.ContextListener
...
class MyAppGrailsPlugin extends Plugin {
...
    Closure doWithSpring() {
        {->
        ...
        httpSessionServletListener(ServletListenerRegistrationBean){
            listener = bean(ContextListener)
        }
        ...
    }
...
}
class SomeService{        
    ...
    def httpSessionServletListener
    ...
    def someMethod(){
        httpSessionServletListener.getSessions()
    }
    ...
}
import javax.servlet.http.HttpSession
import javax.servlet.http.HttpSessionListener

public class ContextListener implements HttpSessionListener {
    ...
    /**
     * All current sessions.
     */
    public List<HttpSession> getSessions() {
        synchronized (this) {
            return new ArrayList<HttpSession>(sessions)
        }
    }
    ...
}
import javax.servlet.http.HttpSession
导入javax.servlet.http.HttpSessionListener
公共类ContextListener实现HttpSessionListener{
...
/**
*所有当前会议。
*/
公共列表getSessions(){
已同步(此){
返回新的ArrayList(会话)
}
}
...
}