【问题标题】:Mixins on Grails Domains Classes broken in upgrade to 2.2Grails Domains 上的 Mixins 在升级到 2.2 时被破坏
【发布时间】:2013-05-24 17:08:33
【问题描述】:

我们有多个使用@Mixin 注解的 Grails 2.0.3 域类

像这样:

@Mixin(PremisesMixin)
class Clinic { 
    Premises premises
    String name
    ....

效果很好!

在尝试更新到 2.2.2 时,mixin 似乎不起作用。我们使用fixtures插件来引导一些数据,在启动过程中,我们得到了与不存在的mixins注入的getter和setter相关的错误。

我确实发现在较新版本的 grails 中 groovy mixins 存在一些问题,但有一个 Grails 特定的替代品http://jira.grails.org/browse/GRAILS-9901

但改成

@grails.util.Mixin(PremisesMixin)
class Clinic { ...

给出其他错误。

Getter for property 'fax' threw exception; nested exception is java.lang.reflect.InvocationTargetException

那么有没有办法在最新版本的 grails 中使用 Grails 域类上的 mixin,还是我需要重构我的代码以避免它们?

更新: src/groovy 中的前提 mixin 如下所示:

class PremisesMixin implements Serializable {
private static final long serialVersionUID = 1L

static fields = ['addressLine1', 'addressLine2', 'city', 'county', 'state', 'postalCode', 'plus4', 'phone', 'latitude', 'longitude']

String getAddressLine1() { premises?.addressLine1 }
void setAddressLine1(String addressLine1) { premises?.addressLine1 = addressLine1 }

String getAddressLine2() { premises?.addressLine2 }
void setAddressLine2(String addressLine2) { premises?.addressLine2 = addressLine2 }

String getCity() { premises?.city }
void setCity(String city) { premises?.city = city }

...

String getPhone() { premises?.phone }
void setPhone(String phone) { premises?.phone = phone }

String getFax() { premises?.fax }
void setFax(String fax) { premises?.fax = fax }
    
    ...
    
    // Workaround for open Groovy bug with Mixins https://issues.apache.org/jira/browse/GROOVY-3612
String toString() {
    this as String
}
}

而前提是这样的:

class Premises {
String addressLine1
String addressLine2
String city
String state
    ...
    
String county
String phone
String fax

Double latitude
Double longitude
}

【问题讨论】:

  • PremisesMixin 长什么样子?我在同一条船上,几个月前升级到 2.2.0,就像你一样,但 Grails @Mixin 为我工作。
  • 已编辑以包含一些前提 mixin 内容
  • 我在静态 fields 中看不到 faxpremises 在哪里。 :)
  • 很好地捕捉到传真不在静态字段中。添加了一些有问题的额外内容,以使事情更清楚。
  • 修复该传真问题并没有解决问题。它仍然无法混合其他 getter 和 setter

标签: grails groovy grails-domain-class


【解决方案1】:

它在 Grails 2.2.2 中适用于我,设置如下:

@grails.util.Mixin(PremisesMixin)
class Clinic {
    String name

    static constraints = {
    }
}

class Premises {
    String fax

    static constraints = {
        fax nullable: true
    }
}

class PremisesMixin {
    //Without this a runtime error is thrown, 
    //like property 'premises' not found in Clinic.
    Premises premises

    void setFax(String fax) {
        premises?.fax = fax
    }
    String getFax() { 
        premises?.fax 
    }
}

//Test Case
def clinic = new Clinic(name: "TestClinic")
clinic.premises = new Premises().save(flush: true, failOnError: true)
clinic.fax = "123456"

clinic.save(flush: true, failOnError: true)

Clinic.list().each{assert it.fax == '123456'}
Premises.list().each{assert it.fax == '123456'}

Mixin 转换的逻辑尚未针对 2.2.x 版本进行修改,尽管我在 master 分支中看到对其进行了修改,但变化很小(使用了通用类文字)。

几个问题:
1. premises 在 mixin 类中是如何访问的?我没有看到它在 Mixin 类中的定义。 2. 实际上你是在什么时候遇到错误的,run-app 或在创建Clinic 期间(类似于上面测试中所做的)?

【讨论】:

  • 场所是在诊所而不是场所 mixin 上定义的,只有 mixin 类上的 getter/setter 我可以尝试将它放在 mixin 上,看看是否有帮助。目前我看到的错误发生在运行应用程序中,当引导程序使用固定插件启动一堆东西时
  • 既然你想为 Clinic、Doc、Nurse 等提供 Premises,那么在 Mixin 中添加 Premises 不是很有用吗?由于这是编译时 Mixin,因此所有这些都应该可以使用前提。还有一件事,我的测试没有使用固定装置,它是普通的代码。
  • 是的,我会在长周末结束后试一试。感谢您的帮助!
  • 运气不好 :( Invalid property 'premises' of bean class [com.Clinic]: Bean property 'premises' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
  • grails 域类中的 Mixin 充其量是参差不齐的,grails 实现与常规实现有一组不同的问题。我个人的看法是为自己省去很多痛苦,并远离 grails 域上的 mixins
猜你喜欢
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 2017-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多