[英]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.