//邮件事件源监听器@ComponentpublicclassMyApplicationListenerimplementsApplicationListener<EmailEven>{@Autowiredprivate EmailController emailController;@OverridepublicvoidonApplicationEvent(EmailEven emailEven){
System.out.println("the evenListener has begin");
emailController.SendEmail(emailEven.getAddress(), emailEven.getMsg());}}
//发短信事件源监听器@ComponentpublicclassMsgApplicationListenerimplementsApplicationListener<MsgEven>{@OverridepublicvoidonApplicationEvent(MsgEven msgEven){
System.out.println("the msgListener has begin");
System.out.printf("send the msg to %s to tell him\n", msgEven.getPhoneNumber());}}
//记录邮件和短信次数监听器@ComponentpublicclassLogApplicationListenerimplementsApplicationListener{@OverridepublicvoidonApplicationEvent(ApplicationEvent applicationEvent){if(applicationEvent instanceofEmailEven){
System.out.println("the logListener has begin");
System.out.println("e_mail record the number to +1");}elseif(applicationEvent instanceofMsgEven){
System.out.println("the logListener has begin");
System.out.println("msg record the number to +1");}else{// System.out.println("other even isn't to record");}}}
4. 模拟业务
@RestControllerpublicclassEmailController{@RequestMapping("/sendEmail")public Boolean SendEmail(@RequestParam String address,@RequestParam String msg){
System.out.printf("send a e-mail to %s,%s\n", address, msg);returntrue;}}
//模拟业务,修改账户成功后,查询账户邮件和手机号码发送邮件和短信@RestControllerpublicclassAccountController{@Autowiredprivate ApplicationContext context;@RequestMappingpublicvoidchangMessage(@RequestParam String newName){
System.out.printf("account change name is %s\n", newName);
String address ="145682qq.com";
String phone ="1651772167";
System.out.printf("query the account the e_mail is %s\n", address);
context.publishEvent(newEmailEven(this,address,"\"account change name is "+ newName));
context.publishEvent(newMsgEven(this,phone));}}