PDA

View Full Version : AfterThrowing and around advice combined



ramin101
06-18-2008, 06:39 PM
Hi all,

I have a need to apply both an AfterThrowing() and an Invoke() to a set of point-cuts. Sometimes (not always) these point-cuts overlap, meaning that both around and throwing advices must be applied. Unfortunately, I'm seeing that if my around advice has been applied, invocation.Proceed() is being called on the target itself and not on the proxy, which means that the AfterThrowing() advice is not being applied. Is there any solution to this problem?:confused:

Thanks in advance.

Ramin

Bruno Baia
06-23-2008, 07:40 AM
Hi,

change the order of your advices :


<property name="interceptorNames">
<list>
<value>throwAdvice</value>
<value>aroundAdvice</value>
</list>
</property>


- Bruno

ramin101
06-27-2008, 07:31 PM
Thanks, Bruno.

In my situation I have one single advice class:



public class MyAdvice : IMethodInterceptor, IThrowsAdvice
{
public object Invoke(IMethodInvocation invocation)
{
...
}

public void AfterThrowing(MethodInfo method, object[] args, object target, Exception ex)
{
...
}
}


and in my web.config I have:



<object id="myAdvice" type="MyAdvice, __Code"/>

<object id="sortRegEx" type="System.Text.RegularExpressions.Regex, System" >
<constructor-arg type="string" value=".*sort$" />
</object>

<object id="mySortProxy" type="Spring.Aop.Support.RegularExpressionMethodPointcut Advisor, Spring.Aop">
<property name="advice">
<ref local="myAdvice"/>
</property>
<property name="pattern">
<ref object="sortRegEx"/>
</property>
</object>


So as you can see I'm applying one advice for both. Am I doing this wrong?

TIA,
Ramin

Bruno Baia
06-30-2008, 10:46 AM
Hi,

Multi advice (advice implementing different interfaces) is not supported atm.

Why not doing everything in IMethodInterceptor implmentation ?


public class MyAdvice : IMethodInterceptor
{
public object Invoke(IMethodInvocation invocation)
{
try
{
return invocation.Proceed();
}
catch(Exception ex)
{
// AfterThrowing stuffs here
}
}
}



- Bruno

ramin101
07-01-2008, 03:31 PM
OK, thanks Bruno, you are right, I could have done that the way you suggest but I wanted to have the proper separation.

BTW, what would the syntax of the xml be if I wanted to have two advices in some order applied to the same regexp? I seem not to be able to figure it out.

Ramin

Bruno Baia
07-06-2008, 11:42 PM
Hi,

Are you using DefaultAdvisorAutoProxyCreator (<aop:config/> ?) to apply your aspects ?

In that case, RegularExpressionMethodPointcutAdvisor implements IOrdered, so you can use the Order property to specify the order :


<object id="mySortProxy" type="Spring.Aop.Support.RegularExpressionMethodPointcut Advisor, Spring.Aop">
<property name="advice">
<ref local="myAdvice"/>
</property>
<property name="pattern">
<ref object="sortRegEx"/>
</property>
<property name="order" value="1">
</object>



Cheers,
Bruno