【问题标题】:Custom annotation not being detected by Spring AOPSpring AOP 未检测到自定义注释
【发布时间】:2013-04-12 19:09:27
【问题描述】:

所以,我一直在尝试使用 Spring AOP,但是一旦我开始使用自定义方法注释,AOP 就会停止工作。

这里是注释:

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 {

}

方面:

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

}

使用自定义注解的类:

package com.test.performers;

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

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

最后是main方法的相关部分。

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

吉他手类正在实现演奏者接口。

我已经环顾了几个小时,我看不出我做错了什么。谢谢!

【问题讨论】:

    标签: java spring annotations aop


    【解决方案1】:

    注解中没有继承。在Guitaritst 类中,当覆盖perform() 方法时,您也应该对其进行注解。

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多