close nav

close nav

Thread: Spring Aop Around Advice Questions

Forum : Spring Core and Aspect Oriented Programming (AOP).   7 May, 2021
User Image
Sriman

Contributor

Joined: 15 Jul, 2016

Points: 1820

Threads: 1913

Posts: 293

Spring Aop Around Advice Questions

| Quote Date : 7 May, 2021    Views:501    

Sharing few questions on Around Advice, looks like important to understand

  • When we should go for Around Advice?
  • How does AOP knows whether the aspect logic we wrote should be applied by using which Advice?
  • How do we access the information about the target class method inside the Advice class Method?
  • Does Around advice method will be called before the Target class method or Instead of Target class method and support with reason whatever it is?
  • If we dont call MethodInvocation.proceed() method does the Target class method would be called or not?
  • Why does the advice method should return a Return Value at the end, can you help?

Comments

User Image
Ramachandr

Newbie

Joined : 19 Oct, 2020

Points : 100

Threads: 3

Posts: 4

Re: Spring Aop Around Advice Questions

Reply Date : 10 May, 2021

01. If we want to apply aspect logic around the primary business logic before and after its execution the we go through with Around Advice.
02. Whenever we are applying around logic, should implement from Method Interceptor interface so AOP by looking that interface it recognize .
03. By taking the help of Reflection apis and also we are implementing from one interface right by overriding the method invoke and invoke carries the Parameters invoke(Method Invocation invocation) with that we can get Method information of target class How means?? just look at the code..


String methodName=invocation.getMathod().getName();

04. Instead of target Class Only.
05. It wont call and control never goes to target class.
06. why bcoz before calling proceed method we can modify the parameters with that modified parameters we called proceed method  and target method execution completes and it returns the value as Object and it not mandatory to modify the parameter values. 
 

User Image
ArviSurya

Newbie

Joined : 9 Oct, 2020

Points : 100

Threads: 0

Posts: 5

Re: Spring Aop Around Advice Questions

Reply Date : 10 May, 2021


    Ques   1> When we should go for Around Advice?

Answere >1:We go for Around Advice if we want to apply the crosscutting/secondary logic around the primary business logic before and After the primary business logic executions
   
 Ques  2>How does AOP knows whether the aspect logic we wrote should be applied by using which Advice?

Answere 2:We have to write our Advice class and implement by standard interface (MethodInterceptor) provide by
Aop and over ride the Object invoke(MethodInvocation methodInvocation) method
 
 Ques 3> How do we access the information about the target class method inside the Advice class Method?

Answere >3: When the Invoke() method has been called by passing MethodInvocation which contains the
information of the currently called method using proxy , so that we can get information about target class method as
Object invoke(MethodInvocation methodInvocation) and MethodInvocation.getArguments()
   
 Ques 4> Does Around advice method will be called before the Target class method or Instead of Target class method and support with reason whatever it is?

Answere >4:Instead of target class method first Aspect class methods get execute and the control given to the target class by invoking
methodinvocation.procedd() method. Aop will pass the details of the target class and its method details on which we want to apply cross cutting logic to method invocation

Ques 5 > If we dont call MethodInvocation.proceed() method does the Target class method would be called or not?

Answere >5: If we dont call MethodInvocation.proceed() method does the Target class method will not be called.


Ques 6>Why does the advice method should return a Return Value at the end, can you help?

Answere >6: After completed the crosscutting logic we have the return value that is returned by the target class method.
 Invoke() method is responsible for returning the return value bcoz invoke() has been called instead of target class
?

User Image
Hariom

Newbie

Joined : 8 Oct, 2020

Points : 100

Threads: 0

Posts: 5

Re: Spring Aop Around Advice Questions

Reply Date : 9 May, 2021

#1.if we want advice aspect logic before the primary logic and after the primary logic then we go through around advice... for
example logging logic is use to trace the flow of execution of application and in our application we use logging in may places then we can say that logging is aspect logic or cross-cutting logic, to trace the application execution flow we need to write the aspect logic before primary logic and after primary logic also. so in this case we use around advice.
#2.by looking the implements interface AOP can identify which advice is used.. for example if we want to use around advice then we need to implements our advice class from MethodInterceptor interface.
#3.when we implements the interface which is use to inform AOP that which advice AOP should use, we need to override method.. for example if we want to use around advice then we need to implements our advice class from methodInterceptor interface in this interface invoke method is there with one parameter that is MethodInvocation it is the class which is use to get information about target class.
#4.around advice method will be called instead of target class method, because when we use around advice then we want to use our cross-cutting logic before and after primary logic, in this case instead of target class method advice class method will called and in the advice class method we call target class method and after completing primary logic control comes again advice class method and after it we can perform cross-cutting logic again and return value to caller. advice class method is return the result to caller because advice class method is called instead of target class method.
#5.if we don't call MethodInvocation.proceed() then control not goes to target class method.
#6.because advice class method is called instead of target class method and target class method return the value to advice class method and advice class method is responsible to return the return value to caller.?

User Image
BsgoudA

Newbie

Joined : 7 Oct, 2020

Points : 100

Threads: 4

Posts: 4

Re: Spring Aop Around Advice Questions

Reply Date : 9 May, 2021

Q) When we should go for Around Advice?
ans: when we want to apply our aspect/cross cutting logic before and after on primary business logic .than we should around Advice
Q) How does AOP knows whether the aspect logic we wrote should be applied by using which Advice?
ans: By looking the our advice class implements from specific advice interfaces like MethodInterceptor those are provide spring         aop, than AOP understood which advice i should apply on target class method.
Q)How do we access the information about the target class method inside the Advice class Method?
ans: By using MethodInvocation class object passed by proxy object while calling currently method of target class method
?? For getting Methodname: methodInvocation.getMethod().getName();
        For getting Parameters :methodInvocation.getArgument();
Q) Does Around advice method will be called before the Target class method or Instead of Target class method and support with reason whatever it is?
  ans: Instead of Target class method it calls the invoke(MethodInvocation invocation) so we write the aspect logic inside this method and   than call primary logic once completed primary logic than it apply again aspect logic with return val
Q) If we dont call MethodInvocation.proceed() method does the Target class method would be called or not?
    ans:  No,it wont call
Q) Why does the advice method should return a Return Value at the end, can you help?
ans: Because of while calling target class method using proxy class object instead of calling the target cls method it called advice method invoke() so after the complete the advice method it is responsible to return the proxy clss object called method

 

Load More

No More Comments