簡體   English   中英

調用帶注釋的方法時運行另一個函數

[英]Run another function when an annotated method is called

有沒有辦法創建自定義,或使用現有的注釋來觸發代碼在調用帶注釋的方法時運行? 最好,我想使用Spring庫。

例如:

@SendEmail("templateName")
public void doSomething() {
    log.info("Something is happening");
}

public void sendEmail(String templateName) {
    // This method is called everytime doSomething() is called
    log.info("Sending email using template " + templateName);
}
@Component
@Aspect
public class Mail {
    @After("execution (@com.yourdirectoryofyourcustomAnnotation.SendMail * *(..))")
    public void sendEmail(JointPoint jp){
        // it will send a mail after every method which tagged by your annotation
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM