Grails 域控制器中的依赖注入

发布于 2024-08-03 00:00:23 字数 1724 浏览 8 评论 0原文

我正在尝试创建自定义约束。我已将逻辑放入服务中:

class RegExpManagerService {

    boolean transactional = false
    def messageSource

    def lookupRegexp(regExpression,Locale locale) {

       def pattern = messageSource.getMessage( regExpression,null,locale )
       return pattern
    }

    def testRegexp(regExpression,text,Locale locale) {
       return text ==~ lookupRegexp(regExpression,locale)
    }
}

并尝试将其注入我的域控制器中:

class Tag extends IbidemBaseDomain {

    def regExpManagerService
    static hasMany=[itemTags:ItemTag]
    static mapping = {
        itemTags fetch:"join"
    }

    //Long id
    Date dateCreated
    Date lastUpdated
    String tag
    // Relation
    Tagtype tagtype
    // Relation
    Customer customer
    // Relation
    Person updatedByPerson
    // Relation
    Person createdByPerson

    static constraints = {
        dateCreated(nullable: true)
        lastUpdated(nullable: true)
        tag(blank: false,validator: {val,obj ->
                regExpManagerService.testRegexp(obj.tagtype.regexpression,val,local)
        })
        tagtype(nullable: true)
        customer(nullable: true)
        updatedByPerson(nullable: true)
        createdByPerson(nullable: true)
    }
    String toString() {
        return "${tag}" 
    }
}

当执行约束时,我收到此错误:

2009-08-24 18:50:53,562 [http-8080-1] ERROR errors.GrailsExceptionResolver  - groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag

I'm trying to create a a custom constraint. I've put the logic in a service:

class RegExpManagerService {

    boolean transactional = false
    def messageSource

    def lookupRegexp(regExpression,Locale locale) {

       def pattern = messageSource.getMessage( regExpression,null,locale )
       return pattern
    }

    def testRegexp(regExpression,text,Locale locale) {
       return text ==~ lookupRegexp(regExpression,locale)
    }
}

and tried to inject it in my domain controller:

class Tag extends IbidemBaseDomain {

    def regExpManagerService
    static hasMany=[itemTags:ItemTag]
    static mapping = {
        itemTags fetch:"join"
    }

    //Long id
    Date dateCreated
    Date lastUpdated
    String tag
    // Relation
    Tagtype tagtype
    // Relation
    Customer customer
    // Relation
    Person updatedByPerson
    // Relation
    Person createdByPerson

    static constraints = {
        dateCreated(nullable: true)
        lastUpdated(nullable: true)
        tag(blank: false,validator: {val,obj ->
                regExpManagerService.testRegexp(obj.tagtype.regexpression,val,local)
        })
        tagtype(nullable: true)
        customer(nullable: true)
        updatedByPerson(nullable: true)
        createdByPerson(nullable: true)
    }
    String toString() {
        return "${tag}" 
    }
}

When the constraint gets executed I get this error:

2009-08-24 18:50:53,562 [http-8080-1] ERROR errors.GrailsExceptionResolver  - groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingPropertyException: No such property: regExpManagerService for class: org.maflt.ibidem.Tag

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2024-08-10 00:00:23

约束闭包是静态的,因此它看不到实例字段“regExpManagerService”。但是您已经验证了该对象,因此您可以从中访问它:

   tag(blank: false,validator: {val,obj ->
      obj.regExpManagerService.testRegexp(obj.tagtype.regexpression,val,local)
   })

The constraints closure is static, so it can't see the instance field 'regExpManagerService'. But you have the object being validated so you can access it from that:

   tag(blank: false,validator: {val,obj ->
      obj.regExpManagerService.testRegexp(obj.tagtype.regexpression,val,local)
   })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文