Post a Job  |   Ask a Question?   |   Already registered? login or register
Basanta Hota
23/01/2016 12:31 AM

Spring Aop ThrowsAdvice

As we know ThrowsAdvice is a marker interface and to access throws advice we need to to write 2 method in implementation class which is standardize method provide by SpringAop... My question is ? why they can't declare that 1 method in directly ThrowsAdvice interface by taking super of Exception like below... interface ThrowsAdvice{ public void afterThrowing(Method method, Object[] args, Object target,Exception e); }
Spam    11 Comments

23/01/2016 02:27:17
As there are many types of exception that is going to be generated ,it is not possible to handle all the types of exception in one single method so if we want to write the cross cutting for all the exceptions that are generated then we have to write many logics in handling that exception in a single method so to help the developers to write their own set of method for which the developers want to write their cross cutting logic. Spring AOP has provided throws advice as a marker interface.we can write our own logic in any no. of method to apply our cross cutting logic, but we can't able to stop that exception to be return to the calle.

23/01/2016 02:40:32
please go through this blog ,i think its helpful..

23/01/2016 14:16:33
Bro Rohit thnx for responses. One more dobut Why Spring Aop provide 2 method to work with Throws advice 1.afterThrowing(Exception e) 2.afterThrowing (Method m,Object[] obj,Object target, Exception e) See both method hve same functionality. But the main role of these 2 method is Just Log or display the Exception not for catch. Then my question is if I can achieve that functionality by using 2nd method then what is the need of first method which contain only reference of ur Exception. ..? I hope you get it wht I want to know?

23/01/2016 15:32:29
suppose assume ThrowsAdvice is not a Marker Interface, In our application there are number of class have different-different methods will rises different exceptions, so that in-side interface end up in writing to multiple overloaded methods for Intercepting Exceptions, not for catching exception. That's why ThrowsAdvice is make it as Marker Interface. so you can write your cross-cutting logic with in the method, but you can not any method, the method should follow the spring Aop specified formate only thats why we can write any methods as u specified in question. I hope you got clear.

23/01/2016 15:54:11
Yup Krishna bro plz read my second question

23/01/2016 16:06:49
ThrowsAdvice{ public void afterThrowing(Method method, Object[] args, Object target,Exception e); } I think, while execution of program it may rise unchecked exception of Error type also then your method does n't work.,in that time ur cross-cutting logic is not performed. Error and its child classes also unchecked Exceptions.

24/01/2016 02:12:31
How could u know. ....? If u write target class then already u know na type of Exception

24/01/2016 03:19:23
if 20 classes are there and all are talking about different-different functionallity...if they are throwing a exception,this will not be possible all are same.ThrowsAdvice is provide a method..this is not possible...why bcoz each exception need to one-one method....i think it will become infinite...becoz checked,unchecked,our own created Exception classes,lot of exception classes are there..."so what he will do...he will given to controll to user,,,write any no. of method but follow some standard signature,,and it will given ThrowsAdvice as a MarkerInterface.... so if any target class throwing a any exception,it will matches in Aspect Advice method what we are writting and it looks into parameter,if same exception then it call appropriate method... it will differenciate these are not a normal method,,these are advice method and inside crosscutting logic is there.

25/01/2016 01:55:27
Bro I know. ..and from ur explanation I learnt something new also. ...but I am not saying wht this interface is marker interface. .....? Plz request to u read question. ..

25/01/2016 09:41:55
Basant afterThrowing() method will be invoked by reflection so u must follow that syntax of writing the afterThrowing() method and u have to write the method in the format void afterThrowing([Method, args[], target, ThrowableSubclass); but first three are optional.--->notice this line It's not that using 2 method we can write but first three are optional so we can write the method in the below format also afterThrowing(Method method,Object target,Exception iae) afterThrowing(Object[] args, Object target,Exception e); afterThrowing(Object target,Exception e); afterThrowing(Object[] args,Exception e); but don't change the order because it will check the arguments as sequential. if u change the order it will give typeMismatch exception. check it. If they fix the method signature and if anyone don't want the information about the target class method still he has to write all the arguments by overriding the method.So they make that as Marker interface and make first three arguments as optional and if u want then write it otherwise don't write. But must follow that method signature. as per my knowledge i understood till now.

26/01/2016 13:31:30
@Tarun all those methods are not working,can you please send a snippet of code,how you work in your application,that is helpful for me...becoz i'm continuousally failing.....

Languages :