【发布时间】:2013-12-30 07:53:12
【问题描述】:
我正在尝试使用 aspectj 从日志文件中排除几种方法(我使用的是 spring 和 Load-time weaving)。有没有办法在 aop.xml 中列出排除的方法?我知道我可以为全班做到这一点,但我正在寻找具体的方法。或者我可以在方面类中列出一个列表吗? 谢谢
【问题讨论】:
标签: aop aspectj spring-aop
我正在尝试使用 aspectj 从日志文件中排除几种方法(我使用的是 spring 和 Load-time weaving)。有没有办法在 aop.xml 中列出排除的方法?我知道我可以为全班做到这一点,但我正在寻找具体的方法。或者我可以在方面类中列出一个列表吗? 谢谢
【问题讨论】:
标签: aop aspectj spring-aop
我不知道如何在 XML 中执行此操作,但在方面本身中执行此操作很容易,因为可以使用布尔运算符组合切入点。
传统aspectj语法:
pointcut whatIDontWantToMatch() : within(SomeClass+) || execution(* @SomeAnnotation *.*(..));
pointcut whatIWantToMatch() : execution(* some.pattern.here.*(..));
pointcut allIWantToMatch() : whatIWantToMatch() && ! whatIDontWantToMatch();
@AspectJ 语法:
@Pointcut("within(SomeClass+) || execution(* @SomeAnnotation *.*(..))")
public void whatIDontWantToMatch(){}
@Pointcut("execution(* some.pattern.here.*(..))")
public void whatIWantToMatch(){}
@Pointcut("whatIWantToMatch() && ! whatIDontWantToMatch()")
public void allIWantToMatch(){}
这些当然只是样本。 whatIDontWantToMatch() 也可以由几个切入点等组成。
【讨论】:
这是在 aspectj 中排除基于 aop.xml 或 aop-ajc.xml 的方法的方法
<aspectj>
<aspects>
<aspect name="com.perf.aspects.EJBAspect"/>
<concrete-aspect name="com.perf.aspects.ConcreteAspectEJBInclude" extends="com.perf.aspects.EJBAspect">
<pointcut name="ejbPointCutInclude"
expression="execution(* com.perf.test.TestClass..*(..))" />
<!-- Methods to exclude for the above execution methods in TestClass. This will also exclude all calls below excluded method -->
<pointcut name="ejbPointCutExclude"
expression="(execution(* com.perf.test.TestClass.getName(..))) || (execution(* com.perf.test.TestClass.getCity(..))) || cflow(execution(* com.perf.test.TestClass.getCity(..))) || cflow(execution(* com.perf.test.TestClass.getName(..))) " />
</concrete-aspect>
</aspects>
</aspectj>
在抽象方面下创建
public abstract aspect EJBAspect
{
public pointcut ejbPointCutInclude();
public pointcut ejbPointCutExclude(): ;
before() : ejbPointCutInclude() && !ejbPointCutExclude()
{
doBefore(thisJoinPointStaticPart.getSignature());
}
after() : ejbPointCutInclude() && !ejbPointCutExclude()
{
doAfter(thisJoinPointStaticPart.getSignature());
}
}
【讨论】: