简体   繁体   English

如何在Grails 3.3.2中为领域类保存方法创建方面?

[英]How to create aspect in Grails 3.3.2 for domain class save method?

I want to create an aspect in grails 3.3.2 that is executed every time that the save() method of an object of my domain class is called. 我想在grails 3.3.2中创建一个方面,该方面在每次调用我的域类的对象的save()方法时执行。

How can I do it? 我该怎么做?

/************/ / ************ /

Edit: @pavger 编辑:@pavger

I have tried the following code, despite calling any method of ProyectoService, my Aspect never runs 我尝试了以下代码,尽管调用了ProyectoService的任何方法,但我的Aspect从未运行

Service Domain Class 服务域类别

import grails.gorm.services.Service

@Service(Proyecto)
interface ProyectoService {

    Proyecto get(Serializable id)

    List<Proyecto> list(Map args)

    Long count()

    void delete(Serializable id)

    Proyecto save(Proyecto proyecto)

}

Aspect! 方面!

@Aspect
class MeAspect {
    @PostConstruct
    public void init() {
        println "Inicializado"
    }

    @Pointcut("within(com.sample.ProyectoService.*())")
    void isDomainClass() {}


    @Around("isDomainClass()")
    Object aroundSaveConnector(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs()
        println "Aspecto before"
        Object object = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs())
        println "Aspecto after"
        return object
    }
}

There is an option, that you create on the Domain class. 您可以在Domain类上创建一个选项。 It is an option on GORM 这是GORM上的一个选项

Events and Auto Timestamping GORM supports the registration of events as methods that get fired when certain events occurs such as deletes, inserts and updates. 事件和自动时间戳记GORM支持将事件注册为在发生某些事件(例如删除,插入和更新)时会触发的方法。 The following is a list of supported events: 以下是受支持的事件的列表:

beforeInsert - Executed before an object is initially persisted to the database. beforeInsert-在对象最初持久保存到数据库之前执行。 If you return false, the insert will be cancelled. 如果返回false,则插入将被取消。

beforeUpdate - Executed before an object is updated. beforeUpdate-在更新对象之前执行。 If you return false, the update will be cancelled. 如果返回false,则更新将被取消。

beforeDelete - Executed before an object is deleted. beforeDelete-在删除对象之前执行。 If you return false, the delete will be cancelled. 如果返回false,则删除将被取消。

beforeValidate - Executed before an object is validated beforeValidate-在验证对象之前执行

afterInsert - Executed after an object is persisted to the database afterInsert-将对象持久保存到数据库后执行

afterUpdate - Executed after an object has been updated afterUpdate-在对象更新后执行

afterDelete - Executed after an object has been deleted afterDelete-删除对象后执行

onLoad - Executed when an object is loaded from the database onLoad-从数据库加载对象时执行

http://gorm.grails.org/6.0.x/hibernate/manual/ http://gorm.grails.org/6.0.x/hibernate/manual/

class Person {
   private static final Date NULL_DATE = new Date(0)

   String firstName
   String lastName
   Date signupDate = NULL_DATE

   def beforeInsert() {
      if (signupDate == NULL_DATE) {
         signupDate = new Date()
      }
   }
}



class Person {

   def securityService

   String firstName
   String lastName
   String lastUpdatedBy

   static constraints = {
      lastUpdatedBy nullable: true
   }

   def beforeUpdate() {
      lastUpdatedBy = securityService.currentAuthenticatedUsername()
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Grails 2.4.5:你如何创建一个 Grails REST controller 没有链接到任何域和一个返回 object 不是域 class 的操作? - Grails 2.4.5: How do you create a Grails REST controller not linked to any domain and an action that returns an object that is not a domain class? 如何创建将实现日志记录功能的方面 class - How to create an aspect class that will implement the logging functionality 如何在从“超级”接口扩展的接口方法上创建方面 - How to create an aspect on an Interface Method that extends from A "Super" Interface 如何在类上创建一个方面,这不是使用Spring AOP的bean? - How to create an aspect on class, that is not a bean using Spring AOP? 如何在数据库中保存类方法? - How to save class method in database? 如何为由同一类中的另一个方法调用的方法运行方面建议 - How to run aspect advice for a method which is called by another method in the same class 方面与实例方法中的类getName()行为 - Class getName() behavior in aspect vs instance method Spring AOP:如何将Caller方法参数值放入Spring Aspect类建议方法 - Spring AOP : How to get Caller method arguments values into Spring Aspect class advice method 尝试从Shell使用Grails域类 - Trying to use a grails domain class from the shell Grails 2 - 无法创建spring安全域对象 - Grails 2 - Cannot create spring security domain object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM