0

I am trying to call the before advice , but the same is not getting executed with the defined pointcut

i have main applicaiton in the package com.my.ms

@SpringBootApplication
@EnableAspectJAutoProxy
public class TemplateServiceApplication {

public static void main(String[] args) {

    SpringApplication.run(TemplateServiceApplication.class, args);
}

}

and in package com.my.ms.tst.advices i have the before advice

@Aspect
public class ValidatingAdvices {

@Before(value = "execution(* com.my.ms.tst.api.*.get*(..))")
public void validateKey(JoinPoint joinPoint) throws Throwable {
     System.out.println("Executing the before advice");
 }

}

the controller lies in the package com.my.ms.tst.api

@Controller
@RequestMapping("/ts")
public class MainController {



@GetMapping("/check")
public String getTemp() throws IOException {

    return "five";
}

}

But the Below Advice is not getting executed

Mohit H
  • 927
  • 3
  • 11
  • 26
  • There is not enough information for a conclusive answer. But if your aspect is not instantiated somewhere as a bean/component, you should help make it being found by Spring's component scanner by adding a `@Component` annotation to the aspect class. – kriegaex Jun 28 '18 at 13:26

2 Answers2

1

You add @Configuration annotation in ValidatingAdvices.

   @Configuration
    @Aspect
    public class ValidatingAdvices {

    @Before(value = "execution(* com.my.ms.tst.api.*.get*(..))")
    public void validateKey(JoinPoint joinPoint) throws Throwable {
         System.out.println("Executing the before advice");
     }

    }
Gaurav Srivastav
  • 2,381
  • 1
  • 15
  • 18
  • What effect is that supposed to have? The answer does not make much sense. – kriegaex Jun 28 '18 at 13:25
  • Have you tried with that. I have executed the code with that change. It works fine. – Gaurav Srivastav Jun 28 '18 at 13:45
  • Sure i will try this , when i am putting the complete method name it is working fine , but when i am using get* , it is not !!. thanks will try the above. – Mohit H Jun 28 '18 at 14:47
  • The `@Configuration` annotation should go to `TemplateServiceApplication` where there actually is some configuration. To put it on the aspect is counterintuitive, even obfuscation. The aspect does not configure anything. You guys should try to understand Spring AOP and not just play around and use whatever seems to work without even understanding why. – kriegaex Jun 28 '18 at 16:21
  • Actually Spring does not scans the Aspect class. That's why we put the @Configuration. – Gaurav Srivastav Jun 28 '18 at 16:35
  • No!!! It is supposed to get a `@Component` annotation in order to get scanned. Isn't anyone reading manuals anymore nowadays? I already mentioned that I my other comment right under the question. – kriegaex Jun 29 '18 at 08:58
0

You can use @Component as well instead of @Configuration or if you used already @component class then it's ok but make sure like ValidatingAdvices class shouldn't have to use @Bean. Click here for more reference.

@Component // @Configuration
@Aspect
public class ValidatingAdvices {

@Before(value = "execution(* com.my.ms.tst.api.*.get*(..))")
public void validateKey(JoinPoint joinPoint) throws Throwable {
     System.out.println("Executing the before advice");
 }

}
vikash
  • 381
  • 2
  • 11