spring - Grails 域类初始化

标签 spring grails groovy grails-orm dynamic-finders

我的 Grails 应用程序在 spring/resources.groovy 中定义了以下 Spring bean

calendarService(CalendarService) { bean ->
    bean.initMethod = "init"     
}

这个方法看起来像:

class CalendarService {
    void init() {
        User.findByEmail("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99fff6f6d9fdf6f6b7faf6f4" rel="noreferrer noopener nofollow">[email protected]</a>")
    }   
}

当我调用动态查找器findByEmail时,我得到一个MissingMethodException。我的猜测是,我试图过早地调用此方法,即在域类将动态查找器添加到其元类之前。一种解决方案是自己从 Bootstrap.init 调用 CalendarService.init(),而不是指示 Spring 调用它,但是有更好的解决方案吗?

谢谢, 唐

最佳答案

你是对的,正如post中所述。 ,如果你需要动态方法,你最好使用 BootStrap.groovy

BootStrap {
    def calendarService
    def init() {
        calendarService.init()
    }
}

关于spring - Grails 域类初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2809712/

相关文章:

java - 将 iCalendar 发送到网页时没有行分隔。 java

spring - org.springframework.batch.item.ReaderNotOpenException : Reader must be open before it can be read

java - groovy 有日期文字吗?

xml - 将 XML 导入 Grails 域类

java - 在 Amazon AWS 上启动 Spring Boot 应用程序,如何设置目标运行时

java - 如何在 spring security 中重定向访问被拒绝的页面?

grails - 在 grails 中保存一对多域关联对象的最佳实践是什么?

hibernate - Grails 默认 Hibernate 缓存配置

testing - 将几个 grails 测试类中使用的可重用测试方法放在哪里

grails - 由于找不到类 Bootstrap 而无法运行Grails