gpt4 book ai didi

java - Spring MVC 自定义事件扩展了 ApplicationEvent 调用两次

转载 作者:行者123 更新时间:2023-12-01 09:50:39 29 4
gpt4 key购买 nike

我已经根据本教程 http://www.baeldung.com/registration-verify-user-by-email 在我的 Spring 应用程序中完成了用户注册电子邮件确认。

遇到一个奇怪的问题。由于未知原因,我的自定义事件总是触发 2 次。

代码如下:事件:

public class OnRegistrationCompleteEvent extends ApplicationEvent {
private final String appUrl;
private final Locale locale;
private final User user;

public OnRegistrationCompleteEvent(User user, Locale locale, String appUrl) {
super(user);

this.user = user;
this.locale = locale;
this.appUrl = appUrl;
}

public String getAppUrl() {
return appUrl;
}
public Locale getLocale() {
return locale;
}
public User getUser() {
return user;
}
}

听众:

@Component
public class RegistrationListener implements ApplicationListener<OnRegistrationCompleteEvent> {

private final static Logger logger = LogManager.getLogger(RegistrationListener.class.getName());


@Autowired
private JavaMailSender mailSender;

@Override
public void onApplicationEvent(OnRegistrationCompleteEvent event) {
this.confirmRegistration(event);
}

private void confirmRegistration(OnRegistrationCompleteEvent event) {
logger.info("OnRegistrationCompleteEvent confirmSocialPosting fired");
final User user = event.getUser();

String recipientAddress = user.getEmail();
String subject = "Registration Confirmation";
String from = "contactblablabla@gmail.com";

MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
String htmlMsg = "Your account was successfully registered! <br><br>" +
"username: "+user.getUsername()+"<br>"+
"password: "+user.getPassword()+"<br>";

mimeMessage.setContent(htmlMsg, "text/html");
helper.setTo(new InternetAddress(recipientAddress));
helper.setSubject(subject);
helper.setFrom(new InternetAddress(from));
mailSender.send(mimeMessage);
} catch (MessagingException e) {
logger.error("Message creation exception: "+e.getMessage());
}
}
}

Controller :

@RequestMapping(value = "/register", method = RequestMethod.POST)
@ResponseBody
public JsonResponse AddUser(@RequestBody @Valid User user, WebRequest request) throws SQLException {
String result = userService.RegisterUser(user);
if(result.equals("done")) {
try {
String appUrl = request.getContextPath();
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(user, request.getLocale(), appUrl));
} catch (Exception me) {
return new JsonResponse("FAIL", "Unknown on event publishing: "+ me.getMessage());
}
return new JsonResponse("OK", "");

} else if(result.equals("duplicate")) {
return new JsonResponse("FAIL", "duplicate");
}
return new JsonResponse("FAIL", "Unknown");
}

因此,在我的日志中,我触发了 OnRegistrationCompleteEvent recognizeSocialPosting 2 次。并发送 2 封电子邮件。可能是什么问题?

最佳答案

这是因为它注册了两次。您在应用程序中使用注释,即 base-package="com.example"这就是它两次注册的原因

从监听器类中删除注释 @component 并在 .xml 文件中定义 bean 定义

这个工作!!

关于java - Spring MVC 自定义事件扩展了 ApplicationEvent 调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630451/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com