加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

挂钩Grails域对象save()

发布时间:2020-12-14 16:32:12 所属栏目:大数据 来源:网络整理
导读:我正在写一个grails插件,我需要挂钩到域save()方法,以便在保存后做一些逻辑.我需要在多个域类中执行此操作.我试图避免在插件用户没有使用带有GORM的hibernate的情况下的hibernate事件. 我已经尝试了很多东西,但下面是我认为应该有最好的工作机会.在所有情况
我正在写一个grails插件,我需要挂钩到域save()方法,以便在保存后做一些逻辑.我需要在多个域类中执行此操作.我试图避免在插件用户没有使用带有GORM的hibernate的情况下的hibernate事件.

我已经尝试了很多东西,但下面是我认为应该有最好的工作机会.在所有情况下,grailsS??ave都为null.我怎样才能做到这一点?

def doWithDynamicMethods = { ctx ->
    application.domainClasses.each { dc ->
        def grailsSave = dc.metaClass.pickMethod('save',[Map] as Class[])

        domainClass.metaClass.save = { Map params ->
        grailsSave.invoke(delegate,[params] as Object[])
        println "Saved object,now do my thing"
        //...
        }
    }
}

我在* Plugin.groovy类中有以下设置:

def dependsOn = [domainClass: '1.1 > *',hibernate: '1.1 > *']
def loadAfter = ['hibernate']

解决方法

在插件/应用程序初始化期间,我无法成功获取对save()方法的引用;我不知道为什么.相反,我决定在插入,更新和删除之后为hibernate事件创建一个监听器. Sean Hartsock关于Audit Logging插件的这个 post是一个完美的底漆.

这是听众的要点:

class MyListener implements PostInsertEventListener,PostUpdateEventListener,PostDeleteEventListener,Initializable {

        public void onPostInsert(final PostInsertEvent event) {
            // logic after insert
            return
        }

        public void onPostUpdate(final PostUpdateEvent event) {
            // logic after update
            return
        }

        public void onPostDelete(final PostDeleteEvent event) {
            // logic after delete
            return
        }


        public void initialize(final Configuration config) {
            return
        }   
    }

然后在* GrailsPlugin.groovy中:

def doWithApplicationContext = { applicationContext ->

    // add the event listeners for reindexing on change
    def listeners = applicationContext.sessionFactory.eventListeners
    def listener = new MyListener()

    ['postInsert','postUpdate','postDelete'].each({
       addEventTypeListener(listeners,listener,it)
    })

}


// copied from http://hartsock.blogspot.com/2008/04/inside-hibernate-events-and-audit.html
private addEventTypeListener(listeners,type) {
    def typeProperty = "${type}EventListeners"
    def typeListeners = listeners."${typeProperty}"

    def expandedTypeListeners = new Object[typeListeners.length + 1]
    System.arraycopy(typeListeners,expandedTypeListeners,typeListeners.length)
    expandedTypeListeners[-1] = listener

    listeners."${typeProperty}" = expandedTypeListeners
}

在一天结束时相当简单……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读