spring事件

Posted diaobiyong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring事件相关的知识,希望对你有一定的参考价值。

DemoEvent 自定义事件 继承ApplicationEvent

/**
 * 自定义事件
 */
public class DemoEvent extends ApplicationEvent {

    private String msg;
    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public DemoEvent(Object source, String msg, String userName) {
        super(source);
        this.msg = msg;
        this.userName  = userName;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

DemoListener 监听 实现ApplicationListener 接口的onApplicationEvent 方法

 @Async
    public void onApplicationEvent(DemoEvent event) {
        //模拟业务耗时
        try {
            System.out.println("ThreadName:"+Thread.currentThread().getName());
            System.out.println("消息:"+event.getMsg()+"发送给了"+event.getUserName());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

DemoPublisher 发布 实现ApplicationEventPublisherAware 接口 的setApplicationEventPublisher 方法 并提供发布方法

public class DemoPublisher implements ApplicationEventPublisherAware{

    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    private ApplicationEventPublisher applicationEventPublisher;

    public  void publish(String msg,String userName){
        applicationEventPublisher.publishEvent(new DemoEvent(this,msg,userName));
    }
}

  App 类

public class App {
    
    private static ClassPathXmlApplicationContext applicationContext;

    public static void main(String[] args) {
        applicationContext = new ClassPathXmlApplicationContext("event/beans.xml");
        final DemoPublisher publisher = (DemoPublisher)applicationContext.getBean("demoPublisher");
        publisher.publish("消息1","张三");
        publisher.publish("消息2","张三");
        publisher.publish("消息3","李四");   
        Thread t1 = new Thread(){
            public void run(){
                publisher.publish("消息11","王五");
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                publisher.publish("消息22","二麻子");
            }
        };
        t1.start();
        t2.start();
        System.out.println("主线程"+Thread.currentThread().getName()+"结束");
        
    }
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="demoListener" class="event.DemoListener"></bean>

    <bean id="demoPublisher" class="event.DemoPublisher"></bean>
</beans>

测试结果

ThreadName:main
消息:消息1发送给了张三
ThreadName:main
消息:消息2发送给了张三
ThreadName:main
消息:消息3发送给了李四
主线程main结束
ThreadName:Thread-0
消息:消息11发送给了王五
ThreadName:Thread-1
消息:消息22发送给了二麻子

异步执行了发送消息

以上是关于spring事件的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

Spring boot:thymeleaf 没有正确渲染片段

如何使用事件侦听器来加载动画片段的循环

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段

Spring Rest 文档。片段生成时 UTF-8 中间字节无效 [重复]