spring - 将应用程序从 grails 2.0.x 升级到 2.4.x

标签 spring grails

我遇到了实现 InitializingBean 接口(interface)的服务类的问题,因此实现了 afterPropertiesSet 方法。然后将服务注入(inject) Controller

在我的例子中,afterPropertiesSet 方法引用了一个调用 .list 方法的域类

在 2.0.4 中一切顺利:在第一次 Controller 操作调用时调用 afterPropertiesSet 并正确执行

在 2.4.2(也在 2.3 中)中,afterPropertiesSet 在应用程序启动时被调用,并因异常而失败

java.lang.IllegalStateException: Method on class [XXXXXX] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.

最佳答案

正如 Graeme Rocher 在此 JIRA 中所阐明的那样:

这是因为在 2.3.x 及更高版本中,默认 Controller 范围是单例的,而在 2.0.x 中,默认范围是原型(prototype)。这里的区别在于,在 2.3.x 中, Controller 是在启动时创建的,而在早期版本中,它仅在第一次请求时创建。

由于动态方法仅在 Spring 上下文初始化后注册,因此当调用 afterPropertySet 方法时,您处于一种中途状态。

但是,您可以通过再次使用以下方法制作 Controller 原型(prototype)来返回之前的行为:

static scope = "prototype"



在 Controller 类中。

关于 Controller 范围的更多信息(以及自 grails 2.3 以来对默认范围的更改)可以在 grails documentation 中找到。

关于spring - 将应用程序从 grails 2.0.x 升级到 2.4.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24563360/

相关文章:

java - 在 Spring MVC 中返回 @Async 方法结果并将其返回给 Ajax 客户端 - 继续

java - Spring RestTemplate : Request/response json not getting logged

Spring SecurityContext 在页面加载后被清除

grails - 如何在Grails War构建中包含simple-json

grails - 奇怪的插入后

java - grails 中面向方面的编程问题。切入点方法未执行

java - 一个 Controller 方法中的 Bean

Grails 不下载插件

grails - Grails 3数据库迁移

没有 Spring Framework 的 Spring Cloud Contract 测试(启动)