简体   繁体   English

Spring AOP未检测到自定义注释

[英]Custom annotation not being detected by Spring AOP

So, I've been trying to play with Spring AOP, but as soon as I start using custom method annotations, the AOP stops working. 因此,我一直在尝试使用Spring AOP,但是一旦我开始使用自定义方法注释,AOP就会停止工作。

Here is the annotation: 这是注释:

package com.test.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

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

}

The Aspect: 方面:

package com.test.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Component
@Aspect
public class Audience {
    @Pointcut("@annotation(com.test.annotations.Performance)")
    public void performance() {
    }

    @Around("performance()")
    public void beforePerformance(ProceedingJoinPoint jointPoint) throws Throwable{
        System.out.println("The audience is getting ready for the show");

        jointPoint.proceed();

        System.out.println("The show is over, audience's leaving");
    }

}

The class using custom annotations: 使用自定义注释的类:

package com.test.performers;

import com.test.annotations.Performance;
import com.test.exceptions.PerformanceException;

public interface Performer {
    @Performance
    void perform() throws PerformanceException;
}

Finally, the relevant part of the main method. 最后是主要方法的相关部分。

Performer kenny = (Performer) context.getBean("guitarist");
kenny.perform();

The Guitarist class is implementing the performer interface. Guitarist类正在实现表演者接口。

I've been looking around for a few hours, I can't see what I'm doing wrong. 我已经环顾了几个小时,看不到我在做什么错。 Thank you ! 谢谢 !

There is no inheritance in annotations. 注释中没有继承。 In Guitaritst class, when overriding the perform() method, you should annotate it as well. Guitaritst类中,当重写perform()方法时,也应对其进行注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM