首页 > 解决方案 > 在建议没有被执行之前

问题描述

我正在尝试调用之前的建议,但没有使用定义的切入点执行相同的建议

我在 com.my.ms 包中有主要应用程序

@SpringBootApplication
@EnableAspectJAutoProxy
public class TemplateServiceApplication {

public static void main(String[] args) {

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

}

在包 com.my.ms.tst.advices 我有之前的建议

@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");
 }

}

控制器位于 com.my.ms.tst.api 包中

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



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

    return "five";
}

}

但是下面的建议没有得到执行

标签: javaspringaop

解决方案


您在ValidatingAdvices中添加 @Configuration 注释。

   @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");
     }

    }

推荐阅读