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");
    }
}
Spring监听器是一种应用于Spring容器的扩展机制,用于监听Spring容器中的事件,当事件发生时执行相应的处理逻辑。Spring监听器可以监听容器中的各种事件,如容器初始化、容器销毁、Bean初始化、Bean销毁等。 创建Spring监听器的方式有两种: 1. 实现ApplicationListener接口 创建一个类实现ApplicationListener接口,并实现onApplicationEvent方法,该方法会在监听到对应的事件时被调用,可以在该方法中编写相应的处理逻辑。 例如: ```java @Component public class MyApplicationListener implements ApplicationListener<MyApplicationEvent> { @Override public void onApplicationEvent(MyApplicationEvent event) { // 处理逻辑 } } ``` 2. 使用@EventListener注解 在方法上添加@EventListener注解,该方法会在监听到对应的事件时被调用,可以在该方法中编写相应的处理逻辑。 例如: ```java @Component public class MyEventListener { @EventListener public void handleMyEvent(MyApplicationEvent event) { // 处理逻辑 } } ``` Spring监听器的作用和使用: 1. 监听容器事件 Spring监听器可以监听容器初始化、容器销毁等事件,可以在事件发生时执行相应的处理逻辑,如打印日志、清理资源等。 2. 监听Bean事件 Spring监听器可以监听Bean初始化、Bean销毁等事件,可以在事件发生时执行相应的处理逻辑,如对Bean进行初始化、销毁前的清理等。 3. 监听自定义事件 Spring监听器可以监听自定义事件,可以在事件发生时执行相应的处理逻辑,如处理用户注册事件、处理订单支付事件等。 总之,Spring监听器是一个非常实用的扩展机制,可以方便地监听Spring容器中的各种事件,并进行相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值