Book Image

Mastering Spring Application Development

By : Anjana Mankale
Book Image

Mastering Spring Application Development

By: Anjana Mankale

Overview of this book

Table of Contents (19 chapters)
Mastering Spring Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Sending Spring mails with AOP


We can also send mails by integrating the mailing functionality with Aspect Oriented Programming (AOP). This can be used to send mails after the user registers with an application. Think of a scenario where the user receives an activation mail after registration. This can also be used to send information about an order placed on an application. Use the following steps to create a MailAdvice class using AOP:

  1. Create a package called com.packt.aop.

  2. Create a class called MailAdvice.

    public class MailAdvice {
      public void advice (final ProceedingJoinPoint proceedingJoinPoint) {
        new Thread(new Runnable() {
        public void run() {
          System.out.println("proceedingJoinPoint:"+proceedingJoinPoint);
          try {
            proceedingJoinPoint.proceed();
          } catch (Throwable t) {
            // All we can do is log the error.
            System.out.println(t);
          }
        }
      }).start();
      }
    }

    This class creates a new thread and starts it. In the run method, the proceedingJoinPoint...