我正在編寫一個方面來記錄控制器中每個 API 調用的請求和響應。我希望能夠在類上使用此注釋,因此使用了 @Target(ElementType.TYPE)之前我已經添加了 @Target(ElementType.Method) 并且我在方法上使用了這個注釋并且它工作正?!,F在我想把它改成@Target(ElementType.TYPE)@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface ReLogger {}@Aspect@Componentpublic class ReLoggerAspect { public static final Logger log = LoggerFactory.getLogger("ReLoggerAspect"); @PostConstruct private void postConstruct() { log.info("ReLoggerAspect Created"); } @Around("@annotation(ReLogger)") private Object reqLoggingAspect(ProceedingJoinPoint joinPoint) throws Throwable { log.info("Request {}",jointPoint.getArgs()[0); }}在類上使用@ReLoggerAspect@RestController@RequestMapping(value = "....", produces = { "application/json" })@ReLoggerpublic class Samplecontroller { /** Some logic here**/.....}調用 API SampleController 時不打印請求
1 回答

一只斗牛犬
TA貢獻1784條經驗 獲得超2個贊
您@annotation
匹配類型注釋的前提是錯誤的,
@within
:將匹配限制為具有給定注釋的類型內的連接點(使用 Spring AOP 時執行在具有給定注釋的類型中聲明的方法)。
@annotation
:將匹配限制為連接點的主題(在 Spring AOP 中執行的方法)具有給定注釋的連接點。
因此,您應該使用@within(fully.qualified.AnnotationType)
.
添加回答
舉報
0/150
提交
取消