0

I have created a spring AOP github link like below. (example1)

but in my output, I see my actual method call has not been executed. as in my first joint cut, I did not invoke proceed but I was expecting the User method to be called as I called proceed in the second one. so I have the below questions

  1. how Spring-aop defines which advice to execute?
  2. how it will decide if there 2 conflicting pointcuts are there, which one to pick up

P.S1: I tried with @Order annotation method and class level but did not help.

P.S2: Based on @kriegaex answer here I updated my code with proceeding both joinpoints, still it is picking one only

kriegaex
  • 63,017
  • 15
  • 111
  • 202
LowCool
  • 1,187
  • 5
  • 25
  • 51
  • Do go through the official documetnation on the ordering : [Advice Ordering](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-ataspectj-advice-ordering) – R.G Mar 16 '23 at 17:21
  • If the issue/concern is still not resolved , do share an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) , which will help others to analyze the code in its entirety and understand the issue/query better. – R.G Mar 16 '23 at 17:23
  • @R.G added full snippet, no ordering did not help – LowCool Mar 16 '23 at 18:59
  • 1
    If possible do share the MCVE through github or so. Snippets wouldn't help here to recreate what you are attempting . For example , `LoggingAspect` is annotated `@Aspect` with no `@Component`, so it is uncertain if it gets registered as a bean , which could be a reason why this code is not working. – R.G Mar 17 '23 at 02:42
  • I agree with R.G. Do yourself a favour and post an [MCVE](https://stackoverflow.com/help/mcve) on GitHub, because then you will get better answers faster instead of 50 iterations of guessing, trial and error. – kriegaex Mar 17 '23 at 09:58
  • @kriegaex added github link – LowCool Mar 17 '23 at 14:03
  • @R.G added github link – LowCool Mar 17 '23 at 14:03
  • I fixed your GitHub link, renamed all packages, invalidating the old link. P.S.: When I asked for an MCVE, I did not mean that you should **replace** all code posted here by just a link. I meant to post the code you think is essential for your question (e.g. aspects and maybe a target class) here and **additionally** link to GitHub, so the people trying to help you can easily clone and run the project with all helper classes, Spring config, Maven etc. – kriegaex Mar 18 '23 at 07:53

1 Answers1

0

Your aspects in example 1 are OK now, both are proceeding. But you only defined one of them as a bean in your XML configuration. Actually, I recommend to get rid of XML configuration altogether and use @Component annotations and component scanning for your aspects and all other Spring components like controllers and services.

Anyway, just add the missing LoggingAspect2 as a bean in your config, and both aspects will be wired and executed.

<bean name="loggingAspect" class="com.luxoft.springaop.example1.LoggingAspect"/>
<bean name="loggingAspect2" class="com.luxoft.springaop.example1.LoggingAspect2"/>

A general hint for you: I think you should give your code more love. It looks a bit chaotic with regard to formatting and method naming.

To simplify aspect logging, I recommend to just log the joinpoint and maybe the aspect name for debugging/tracing purposes, if you have multiple advices targeting the same methods. Reserve elaborate text messages for cases in which you really need them. The joinpoint contains information about the type of joinpoint (always execution in Spring AOP, only native AspectJ knows more pointcut types) and the intercepted method's signature.

Just like in my answer to your other question, I also suggest to simply use @Before or @After advice types instead of the more complex @Around, because for simple logging that should be enough. No proceeding, no return values, clean and easy.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Thank you for your answer @kriegaex. my actual scenario is more complicated actually. I just created gist, weirdly when I was working, both aspect were in same class but somehow they didn't pick up now it is working fine – LowCool Mar 18 '23 at 12:40