亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

攔截 Spring AOP 或 AspectJ 中帶注解的類和方法

攔截 Spring AOP 或 AspectJ 中帶注解的類和方法

GCT1015 2023-01-05 17:01:22
所以我有一個自定義注釋@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Inherited@Documentedpublic @interface Intercepted {}我想用它來將方面編織到方法中(AspectJ,@annotation(Intercepted))。這個想法是,當我@Intercepted直接注釋方法時,我將方面編織進去——那部分工作——或者如果我注釋類,應該將方面編織到它的所有(公共)方法中——那部分不起作用。此外,如果我注釋一個類及其方法之一,則方面應該只編織一次,方法級別的注釋覆蓋類級別的注釋。本質上,我想要一個“如果有類級注釋,則添加類級注釋,但前提是還沒有方法級注釋”。我怎么做?
查看完整描述

1 回答

?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

這是一個 AspectJ 示例。切入點語法在 Spring AOP 中是相同的。


幫助類:


package de.scrum_master.app;


import java.lang.annotation.*;


@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

@Documented

public @interface Intercepted {}

package de.scrum_master.app;


@Intercepted

public class AnnotatedClass {

  public void doSomething() {}

  public void doSomethingElse() {}

}

package de.scrum_master.app;


public class AnnotatedMethod {

  @Intercepted

  public void doSomething() {}

  public void doSomethingElse() {}

}

package de.scrum_master.app;


@Intercepted

public class AnnotatedMixed {

  @Intercepted

  public void doSomething() {}

  public void doSomethingElse() {}

}

驅動程序應用程序(Java SE,無 Spring):


package de.scrum_master.app;


public class Application {

  public static void main(String[] args) {

    // Should be logged

    new AnnotatedClass().doSomething();

    // Should be logged

    new AnnotatedClass().doSomethingElse();


    // Should be logged

    new AnnotatedMethod().doSomething();

    // Should NOT be logged

    new AnnotatedMethod().doSomethingElse();


    // Should be logged, but only once

    new AnnotatedMixed().doSomething();

    // Should be logged

    new AnnotatedMixed().doSomethingElse();

  }

}

方面:


請注意,該execution(* *(..)) &&部分在 Spring AOP 中不是必需的,因為那里僅支持方法執行連接點。切入點可能就在annotatedMethod() || annotatedClass()那里。在 AspectJ 中,我必須更加精確,否則會記錄其他連接點類型。


package de.scrum_master.aspect;


import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;


@Aspect

public class AnnotationInterceptor {

  @Pointcut("@annotation(de.scrum_master.app.Intercepted)")

  public void annotatedMethod() {}


  @Pointcut("@within(de.scrum_master.app.Intercepted)")

  public void annotatedClass() {}


  @Before("execution(* *(..)) && (annotatedMethod() || annotatedClass())")

  public void log(JoinPoint thisJoinPoint) {

    System.out.println(thisJoinPoint);

  }

}

控制臺日志:


execution(void de.scrum_master.app.AnnotatedClass.doSomething())

execution(void de.scrum_master.app.AnnotatedClass.doSomethingElse())

execution(void de.scrum_master.app.AnnotatedMethod.doSomething())

execution(void de.scrum_master.app.AnnotatedMixed.doSomething())

execution(void de.scrum_master.app.AnnotatedMixed.doSomethingElse())


查看完整回答
反對 回復 2023-01-05
  • 1 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號