调用带注释的方法时,运行另一个函数 [英] Run another function when an annotated method is called

查看:61
本文介绍了调用带注释的方法时,运行另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以创建自定义或使用现有的注释来触发代码,以便在调用带注释的方法时运行代码?最好,我想使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆