java - 自定义注释方面不起作用

标签 java spring aspectj spring-aop

我正在尝试使用 @Around 建议为自定义注释编写方面,但有些原因它不调用。 这是我的代码:

@Aspect
@Component
public class AspectMeasureTime {
    @Around(value = "execution(* com.beh.businesslayer..*(..)) && @annotation(measureTime)")
    public void measureTime(ProceedingJoinPoint pjp, MeasureTime measureTime) throws Throwable {
    /*some code*/
    }
}

@Configuration
@ComponentScan(basePackages = {"com.beh"})
@EnableAspectJAutoProxy(proxyTargetClass = false)
public class WebConfig {
}

package com.beh.businesslayer.businessworkflows.pagefetcher;
public class PageFetcherWriter implements ItemWriter, StepExecutionListener {
    @MeasureTime("timee")
    private void printString(Object s) {
        logger.info("entry = " + s);
    }
}

package com.beh.businesslayer.businessworkflows.pagefetcher;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MeasureTime {
    String value();
}

我尝试在没有此注释的情况下创建连接点,它工作正常。尝试使用@annotation(com.beh.businesslayer.businessworkflows.pagefetcher.MeasureTime),它也不起作用。您知道为什么以及它应该是什么样子吗?

最佳答案

如果你要传递一个参数,那么你需要明确地告诉 AspectJ 它正在被传递

@Around(value = "execution(* com.beh.businesslayer..*(..)) && @annotation(measureTime) && args(measureTime)")
public void measureTime(ProceedingJoinPoint pjp, MeasureTime measureTime) throws Throwable {

关于java - 自定义注释方面不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36106277/

相关文章:

java:如何复制当前正在执行的文件

java - 单例类中的静态类也是单例吗?

spring - 使用 Spring security 和 Thymeleaf - Spring 根据角色重定向页面

java - AspectJ 切入点表达式匹配任意位置的参数注释

Spring AOP 不适用于 Feign Client

java - 获取获取切入点内访问字段的值

java - 解析来自 google 查询的 HTML 链接

java - 如何在 Java 中将 String 转换为 Boolean,但将 null 与 false 区别对待?

java - 限制服务器使用供内部使用

java - Spring组件注解编译时扫描