0

As per my understanding, @within would execute if an annotation is at the class level so as per that understanding below snippet should run the @within block.

example6

but my output did not print @within advice values. why?

LowCool
  • 1,187
  • 5
  • 25
  • 51
  • Please learn [how to ask a question on SO](https://stackoverflow.com/help/how-to-ask) and provide a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve). Thank you. There are classes missing here. For questions needing many files, ideally just post a link to a GitHub project. – kriegaex Mar 17 '23 at 10:13
  • Only issue I am having with github that I need to recreate a new project, and push to my personal repo, that would be monitored in my org – LowCool Mar 17 '23 at 12:05
  • That should not be any problem. I do not need your original, confidential code, but a minimal reproducer, as described in the MCVE article, which you do not seem to have read. You can rename and anonymise everything, as long as it reproduces the issue. I guess, your company will be happy that you are trying to solve the problem instead of wasting your work time being stuck. – kriegaex Mar 17 '23 at 13:30
  • @kriegaex added github link. – LowCool Mar 17 '23 at 14:05
  • Thanks. Actually, I was hoping for something including Maven or Gradle build configuration, because now I have to guess your Spring and dependency versions. I can take a look and will tell you, if I have any problems. – kriegaex Mar 17 '23 at 14:22
  • @kriegaex it has pom.xml here :https://github.com/vihang16/AOPEXamples/tree/master/examples/M4-AOP – LowCool Mar 17 '23 at 14:30
  • Oh sorry, I was looking in the root directory. Great, thanks. – kriegaex Mar 17 '23 at 14:53

1 Answers1

0

Your main class only calls testService.test, but that method is not annotated by @Log. So why are you expecting it to be intercepted by the aspect? Also, there is only class AnnotatedWithinTestImpl in the within package, but you are not executing any code from it.

Adding

AnnotatedWithinTestImpl annotatedWithinTest = (AnnotatedWithinTestImpl) context.getBean("annotatedWithinTest");
annotatedWithinTest.test();

to your main method should trigger the testAllBeansWithAnnotation advice and print:

WITHIN ANNOTATION LOG: CALL METHOD (...) within.AnnotatedWithinTestImpl#test with args []

For me, locally that works after adding the missing pieces of your code snippets.


Update: Thanks for the reproducer on GitHub. Now I can really reproduce and see what you did wrong. You use an @Around advice without calling jp.proceed(), i.e. the target method execution is blocked in the first advice. The second one therefore never fires. So

  • either make sure that both advices proceed and return the proceed result,
  • or, because you neither want to modify method parameters nor the method result, just switch from @Around to a simpler @Before or @After advice, then it works like a charm and there is no need to proceed or return anything.

This is all described in the Spring AOP manual.

The second option looks like this:

    @Order(2)
    @Before("allBeans()")
    public void testAllBeans(JoinPoint jp) {
        LOGGER.setLevel(Level.FINEST);
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setLevel(Level.FINE); // set console handler logging level to FINE
        LOGGER.addHandler(consoleHandler);
        String type = jp.getSignature().getDeclaringTypeName();
        String methodName = jp.getSignature().getName();
        Object[] methodArgs = jp.getArgs();
        print("WITHIN LOG: CALL METHOD " + type + "#" + methodName + " with args " + Arrays.toString(methodArgs));
    }

    @Order(1)
    @Before("allBeansWithAnnotation()")
    public void testAllBeansWithAnnotation(JoinPoint jp) {
        LOGGER.setLevel(Level.FINEST);
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setLevel(Level.FINE); // set console handler logging level to FINE
        LOGGER.addHandler(consoleHandler);
        String type = jp.getSignature().getDeclaringTypeName();
        String methodName = jp.getSignature().getName();
        Object[] methodArgs = jp.getArgs();
        print("WITHIN ANNOTATION LOG: CALL METHOD " + type + "#" + methodName + " with args " + Arrays.toString(methodArgs));
    }

The console log then is:

WITHIN LOG: CALL METHOD com.springaop.example6.within.WithinTest#test with args []
WITHIN ANNOTATION LOG: CALL METHOD com.springaop.example6.within.WithinTest#test with args []

P.S.: Next time, please test your MCVE. You still had some old package names in your pointcuts, pointing to bogus packages.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • I am getting only `WITHIN LOG: CALL METHOD` part, am not getting the annotation part log, seems it is an issue with 2 matching pointcuts, I updated my code with your snippet – LowCool Mar 17 '23 at 12:38
  • Please note my updated answer explaining your mistake and suggesting two ways to fix it. – kriegaex Mar 17 '23 at 15:16
  • but by that logic, question I asked https://stackoverflow.com/questions/75758765/spring-aop-proxy-call should have solved, but it didn't work there – LowCool Mar 17 '23 at 16:12