我遇到了实现 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/