您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

spring监听器用法

1. 新建springboot项目,2.3.7.RELEASE

2.自定义事件源,继承ApplicationEvent

//邮件发送事件源
public class EmailEven extends ApplicationEvent {
    
    private String address;
    private String msg;

    public EmailEven(Object source, String address, String msg) {
        super(source);
        this.address = address;
        this.msg = msg;
    }

    public String getAddress() {
        return address;
    }

    public String getMsg() {
        return msg;
    }
}
//短信发送事件源
public class MsgEven extends ApplicationEvent {
    private String phoneNumber;

    public MsgEven(Object source, String phoneNumber) {
        super(source);
        this.phoneNumber = phoneNumber;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }
}

3. 自定义监听器,实现 ApplicationListener,如果T传入了事件源则监听该事件源,如MyApplicationListener和MsgApplicationListener,否则监听所有事件,包括容器已有事件源,如LogApplicationListener

//邮件事件源监听器
@Component
public class MyApplicationListener implements ApplicationListener<EmailEven> {
    @Autowired
    private EmailController emailController;

    @Override
    public void onApplicationEvent(EmailEven emailEven) {
        System.out.println("the evenListener has begin");
        emailController.SendEmail(emailEven.getAddress(), emailEven.getMsg());
    }
}
//发短信事件源监听器
@Component
public class MsgApplicationListener implements ApplicationListener<MsgEven> {
    @Override
    public void onApplicationEvent(MsgEven msgEven) {
        System.out.println("the msgListener has begin");
        System.out.printf("send the msg to %s to tell him\n", msgEven.getPhoneNumber());
    }
}
//记录邮件和短信次数监听器
@Component
public class LogApplicationListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (applicationEvent instanceof EmailEven){
            System.out.println("the logListener has begin");
            System.out.println("e_mail record the number to +1");
        }else if (applicationEvent instanceof MsgEven){
            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. 模拟业务

@RestController
public class EmailController {
    @RequestMapping("/sendEmail")
    public Boolean SendEmail(@RequestParam String address, @RequestParam String msg){
        System.out.printf("send a e-mail to %s,%s\n", address, msg);
        return true;
    }
}
//模拟业务,修改账户成功后,查询账户邮件和手机号码发送邮件和短信
@RestController
public class AccountController{
    @Autowired
    private ApplicationContext context;
    @RequestMapping
    public void changMessage(@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(new EmailEven(this,address,"\"account change name is " + newName));
        context.publishEvent(new MsgEven(this,phone));
    }
}
  1. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest {
    @Autowired
    private EmailController emailController;

    @Autowired
    private ApplicationContext context;

    @Autowired
    private AccountController accountController;

    @Test
    public void test1(){
        accountController.changMessage("jim");
    }
}

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进