原创

AspectJ @Pointcut 和 @AfterReturning 基于方法注释未被触发

温馨提示:
本文最后更新于 2024年04月12日,已超过 48 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I have other ways to accomplish this, but I've never done any AspectJ so I'm trying such an approach.

I want to annotate various getters such that their results are always cast to lowercase:

@AlwaysLower
@JsonProperty("clinic_id")
public String getClinicId() {
    return clinicId;
}

I have the annotation defined as such:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AlwaysLower {
}

And here's my @Aspect class:

@Aspect
@Configuration
public class AlwaysLowerAspect {
    
    private static final Logger LOG = LoggerFactory.getLogger(AlwaysLowerAspect.class);
    
    public AlwaysLowerAspect() {
        super();
        LOG.info("AlwaysLowerAspect constructed!");
    }

    @Pointcut("@annotation(com.mycompany.mypackage.AlwaysLower)")
    public void annotatedWithAlwaysLower() {}

    @AfterReturning(pointcut = "annotatedWithAlwaysLower()", returning = "returnValue")
    public void convertToLowercase(Object returnValue) {
        if (returnValue instanceof String) {
            String original = (String) returnValue;
            if (original != null) {
                returnValue = original.toLowerCase();
            }
        }
    }
}

At runtime, I see the constructor fire, but neither of the annotated methods within my @Aspect are triggered. I am using a fake package name, here, but the correct one is in my code and I'm getting nothing. Am I trying to accomplish the impossible, and/or what am I doing wrong?

正文到此结束
热门推荐
本文目录