调用带注释的方法时运行另一个函数

时间:2016-05-04 10:53:15

标签: java spring annotations

有没有办法创建自定义,或使用现有的注释来触发在调用带注释的方法时运行的代码?最好,我想使用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);
}

1 个答案:

答案 0 :(得分:0)

@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
    }
}