在Grails中注册为Spring Bean而不是web.xml的Servlet和过滤器 [英] Servlets and Filters registered as Spring Beans instead of web.xml in Grails

查看:87
本文介绍了在Grails中注册为Spring Bean而不是web.xml的Servlet和过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Grails中从2.x升级到3.0.6"在文档中,已经注意到新的servlet和过滤器可以分别注册为Spring Bean,也可以分别注册为ServletRegistrationBean和FilterRegistrationBean",但是在这件事上没有多说.

In the Grails 'Upgrading from 2.x to 3.0.6' document it's been noted that "new servlets and filters can be registered as Spring beans or with ServletRegistrationBean and FilterRegistrationBean respectively" however not much else is said on the matter.

我想知道是否有人对如何正确执行此操作有任何好的意见(即,使用包含servlet上下文的init/BootStrap.groovy来加载bean,而使用conf/spring中的bean),或者是否存在一些预定义Spring的方式很明显,我很想念.

I am wondering if anybody has any good input on how to do this properly (i.e., using the init/BootStrap.groovy which contains the servlet context to load beans, versus beans in conf/spring) or perhaps there is some pre-defined Spring way of doing this that's obvious and I am missing.

注意:我正在尝试将spring-ws集成到Grails 3.0.6中.

Note: I am trying to integrate spring-ws into Grails 3.0.6.

推荐答案

对于插件,应在doWithSpring中进行;对于应用程序,应在grails-app/conf/spring/resources.groovy中进行.由于Grails 3基于Spring Boot,因此您也可以使用@Bean方法.

You should do this in doWithSpring for a plugin, or grails-app/conf/spring/resources.groovy for an app. Since Grails 3 is based on Spring Boot you can also use an @Bean method.

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

When the application context starts up, Spring looks for ServletRegistrationBeans, FilterRegistrationBeans, etc. and uses their configured properties to do programmatic registration in the servlet container for you.

Grails源代码中有一些示例. ControllersGrailsPlugin注册了一些过滤器(例如

There are some examples in the Grails source. ControllersGrailsPlugin registers some filters (e.g. here) and the main dispatcher servlet is registered here.

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

There's some documentation in the Spring Boot docs although it's biased towards @Bean methods, but you can use any approach to defining the beans.

这篇关于在Grails中注册为Spring Bean而不是web.xml的Servlet和过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆