调用带注释的方法时运行另一个函数 [英] Run another function when an annotated method is called
本文介绍了调用带注释的方法时运行另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法创建自定义或使用现有的注释来触发代码在调用带注释的方法时运行?最好,我想使用 Spring 库.
Is there a way to create a custom, or use an existing, annotation to trigger code to run when the annotated method is called? Preferably, I would like to use Spring libraries.
例如:
@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
}
}
这篇关于调用带注释的方法时运行另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文