在 Scala 中使用 aspectj 修改方法参数值
Modify a method parameter value using aspectj in Scala
我有一种情况需要在scala中使用aspectj修改方法参数值。我正在使用 @Before 注释来获取字段。
@Before("execution (* com.myapp.EmployeeController.delete(..))")
def checkIfWorkflowEnabled(joinPoint: ProceedingJoinPoint): Object = {
//get the fields and modify oen field
//check some condition and if success, update the isActive field to false
//then proceed with what was going on before
}
我的控制器方法:
def delete(id:Long, isActive:Boolean)= {
???
}
但我无法使用 aspectj 更新字段 'isActive'。
编辑:
正如Andy所说,我了解到我们只能在around方法中修改方法args。现在,我有另一个疑问。在around方法中,切面会在实际方法调用前后执行吗?如何在删除方法执行之前修改方法参数,然后照常进行。在删除方法完成后,调用其他方法。?
before() 和 after() 建议不能修改连接点上将要发生的事情,它们只能观察它。 around() 建议会做你需要的。类似于:
void around(long l,boolean b): execution(* delete(..)) && args(l,b) {
if ((l%2)==0) {
proceed(l,b);
} else {
proceed(l,!b);
}
}
对不起,我用代码风格写的,我只是更熟悉它。这可以通过注释样式实现,但您需要使用附加变量 ProceedingJoinPoint
来调用继续:http://www.eclipse.org/aspectj/doc/released/adk15notebook/ataspectj-pcadvice.html
我有一种情况需要在scala中使用aspectj修改方法参数值。我正在使用 @Before 注释来获取字段。
@Before("execution (* com.myapp.EmployeeController.delete(..))")
def checkIfWorkflowEnabled(joinPoint: ProceedingJoinPoint): Object = {
//get the fields and modify oen field
//check some condition and if success, update the isActive field to false
//then proceed with what was going on before
}
我的控制器方法:
def delete(id:Long, isActive:Boolean)= {
???
}
但我无法使用 aspectj 更新字段 'isActive'。
编辑:
正如Andy所说,我了解到我们只能在around方法中修改方法args。现在,我有另一个疑问。在around方法中,切面会在实际方法调用前后执行吗?如何在删除方法执行之前修改方法参数,然后照常进行。在删除方法完成后,调用其他方法。?
before() 和 after() 建议不能修改连接点上将要发生的事情,它们只能观察它。 around() 建议会做你需要的。类似于:
void around(long l,boolean b): execution(* delete(..)) && args(l,b) {
if ((l%2)==0) {
proceed(l,b);
} else {
proceed(l,!b);
}
}
对不起,我用代码风格写的,我只是更熟悉它。这可以通过注释样式实现,但您需要使用附加变量 ProceedingJoinPoint
来调用继续:http://www.eclipse.org/aspectj/doc/released/adk15notebook/ataspectj-pcadvice.html