grails - 为所有 GORM 对象添加属性和方法

标签 grails grails-orm abstract-syntax-tree

我希望能够添加以下属性 updatedBy
我的大部分 GORM 对象。我正在考虑使用 AST 转换。类似于此处描述的内容:http://www.zorched.net/tag/grails/

但我需要在 gorm Hook 上设置此属性 beforeUpdate()beforeInsert()就像是

def beforeUpdate() {
    updatedBy = springSecurityService.currentUser;
}

这就是我挣扎的地方。任何帮助表示赞赏?

最佳答案

将其封装为每个域类实现或扩展的特征或抽象类。如果您使用的是足够新的 Groovy 版本以支持特征,则最好是特征。如果没有,像这样的抽象类

class Audited {
  def springSecurityService
  def updatedBy

  def beforeUpdate() {
    updatedBy = springSecurityService.currentUser
  }
}

关于grails - 为所有 GORM 对象添加属性和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242706/

相关文章:

sorting - <=>在grails排序中是什么意思

postgresql - Grails/Gorm 和 Postgresql Unaccent

haskell - 在Haskell中选择AST表示形式

java - 什么 Javascript 框架与 Grails 集成得很好?

sql - 动态访问grails中groovy类中的DataSource

Grails GORM : list all with nested property

java - GORM 对象关系未加载到异步 block 中

GoLang ast : generating and printing a tree without position information

ruby - 树顶语法无限循环

Grails - OSIV - 停止 "Open Session In View"静态资源过滤器