我想在grails 3.3.2中创建一个方面,该方面在每次调用我的域类的对象的save()
方法时执行。
我该怎么做?
/ ************ /
编辑:
@pavger
我尝试了以下代码,尽管调用了ProyectoService的任何方法,但我的Aspect从未运行
服务域类别
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
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
}
}
最佳答案
您可以在Domain类上创建一个选项。
这是GORM上的一个选项
Activity 和自动时间戳
GORM支持将事件注册为在发生某些事件(例如删除,插入和更新)时触发的方法。以下是受支持的事件的列表:
beforeInsert-在对象最初持久保存到数据库之前执行。如果返回false,则插入将被取消。
beforeUpdate-在更新对象之前执行。如果返回false,则更新将被取消。
beforeDelete-在删除对象之前执行。如果返回false,则删除将被取消。
beforeValidate-在验证对象之前执行
afterInsert-将对象持久保存到数据库后执行
afterUpdate-在对象更新后执行
afterDelete-删除对象后执行
onLoad-从数据库加载对象时执行
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()
}
}
关于spring - 如何在Grails 3.3.2中为领域类保存方法创建方面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48652477/