Home How to create aspect in Grails 3.3.2 for domain class save method?
Reply: 1

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

Grd_92 Published in 2018-02-06 21:44:41Z

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.

How can I do it?


Edit: @pavger

I have tried the following code, despite calling any method of ProyectoService, my Aspect never runs

Service Domain Class

import grails.gorm.services.Service

interface ProyectoService {

    Proyecto get(Serializable id)

    List<Proyecto> list(Map args)

    Long count()

    void delete(Serializable id)

    Proyecto save(Proyecto proyecto)



class MeAspect {
    public void init() {
        println "Inicializado"

    void 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
Marco Araujo
Marco Araujo Reply to 2018-02-07 03:47:03Z

There is an option, that you create on the Domain class. It is an option on 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. The following is a list of supported events:

beforeInsert - Executed before an object is initially persisted to the database. If you return false, the insert will be cancelled.

beforeUpdate - Executed before an object is updated. If you return false, the update will be cancelled.

beforeDelete - Executed before an object is deleted. If you return false, the delete will be cancelled.

beforeValidate - Executed before an object is validated

afterInsert - Executed after an object is persisted to the database

afterUpdate - Executed after an object has been updated

afterDelete - Executed after an object has been deleted

onLoad - Executed when an object is loaded from the database


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()
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.367406 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO