title: ApplicationListener invoked twice
body: <p>I'm doing an Spring MVC webapp, then I want to send an email to new registered user in confirm / activation purposes. Then I write an ApplicationEvent and ApplicationListener&lt;> classes. The problem is that, listener is going to invoke twice, so I receive 2 emails, and have 2 token strings in my database. </p>
  
  <p>How to solve this problem?</p>
  
  <p>Code below,</p>
  
  <p>ApplicationEvent:</p>
  
  <pre><code>public class OnRegistrationSuccessEvent extends ApplicationEvent {
  
      private String appUrl;
      private Locale locale;
      private User user;
  
      public OnRegistrationSuccessEvent(String appUrl, Locale locale, User user) {
          super(user);
          this.appUrl = appUrl;
          this.locale = locale;
          this.user = user;
      }
  
      // getters, setters
  }
  </code></pre>
  
  <p>ApplicationListener:</p>
  
  <pre><code>@Component
  public class RegistrationListener implements ApplicationListener&lt;OnRegistrationSuccessEvent&gt; {
  
      @Autowired
      UserService userService;
  
      @Autowired
      MessageSource messages;
  
      @Autowired
      JavaMailSender javaMailSender;
  
      @Override
      public void onApplicationEvent(OnRegistrationSuccessEvent event) {
          this.confirmationEmail(event);
      }
  
      private void confirmationEmail(OnRegistrationSuccessEvent event){
         // register token in DB, send mail
      }
  
      private SimpleMailMessage buildEmailMessage(OnRegistrationSuccessEvent event, User user, String token) {
         //build some message
      }
  }
  </code></pre>
  
  <p>I invoke the event in @Controller with:</p>
  
  <pre><code>eventPublisher.publishEvent(new OnRegistrationSuccessEvent(URL, request.getLocale(), user));
  </code></pre>
  
  <p>Thank you for help,</p>