You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

@Timed注解触发AOP切面方法失效问题求助

Troubleshooting Why @Timed Annotation Isn't Triggering Your AOP Aspect

Hey there, let's get to the bottom of why your MyTracer aspect isn't firing when methods annotated with @Timed run. I’ve looked over your code, and here are the key checks and fixes to try:

  • Make sure your aspect is managed by Spring
    Your MyTracer class has the @Aspect annotation, but Spring won’t detect it as a bean unless you add a component stereotype annotation like @Component, or explicitly register it via a @Bean method in a configuration class. Update your class like this:

    @Aspect
    @Component // Add this annotation
    public class MyTracer {
        // Your existing code here
    }
    
  • Enable AspectJ auto-proxying
    Spring needs to know to process aspect-oriented programming logic. Add the @EnableAspectJAutoProxy annotation to your main application class or a configuration class. For Spring Boot, this looks like:

    @SpringBootApplication
    @EnableAspectJAutoProxy // Enable AOP proxying
    public class YourApplication {
        public static void main(String[] args) {
            SpringApplication.run(YourApplication.class, args);
        }
    }
    
  • Verify the @Timed annotation’s package and retention policy
    Double-check you’re using the correct @Timed annotation (e.g., Micrometer’s io.micrometer.core.annotation.Timed if that’s what you intended). If you created a custom @Timed annotation, ensure it has the runtime retention policy so Spring can detect it at runtime:

    @Retention(RetentionPolicy.RUNTIME) // Required for AOP detection
    @Target(ElementType.METHOD)
    public @interface Timed {
        // Your annotation attributes
    }
    
  • Ensure the annotated method belongs to a Spring-managed bean
    AOP only works on methods in classes that are Spring beans. If the method with @Timed is in a class that isn’t marked with @Component, @Service, @Repository, or another stereotype annotation, Spring won’t proxy it. Make sure the target class is a Spring bean.

  • Check for internal method call limitations
    If you’re calling the @Timed method from within the same class (internal method call), Spring’s dynamic proxy won’t intercept it. This is because the proxy only handles external calls to the bean. To fix this, you can:

    • Inject the bean into itself (using @Autowired and @Lazy to avoid circular dependencies)
    • Retrieve the bean from the ApplicationContext and call the method through it
    • Switch to compile-time weaving with AspectJ (more complex, but works for internal calls)
  • Explicitly specify the annotation in your pointcut
    To avoid any ambiguity with other @Timed annotations from different packages, update your pointcut to use the full class name of the annotation:

    @Around("@annotation(io.micrometer.core.annotation.Timed)") // Use full path
    public Object trackExecutionTime(ProceedingJoinPoint proceedingJoinPoint, Timed timed) throws Throwable {
        // Your existing logic here
    }
    

After trying these steps, your AOP aspect should trigger whenever a method annotated with @Timed is executed.

内容的提问来源于stack exchange,提问作者Neil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:28:34