spring自定义事件和事件监听器以及事件的发布-ApplicationEvent

进行spring自定义事件步骤:

1、继承ApplicationEvent自定义事件;

2、实现接口ApplicationListener定义事件监听器;

3、使用ApplicationContext来发布事件;

 

一、继承ApplicationEvent自定义事件

      代码如下:

/**
 * 自定义事件
 * @author lyq
 */
public class DemoEvent extends ApplicationEvent {
    private String msg;

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

    public String getMsg() {
        return msg;
    }

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

 

二、实现接口ApplicationListener定义事件监听器

      代码如下:

/**
 * 定义事件监听器
 *      实现ApplicationListener接口,指定监听的事件类型
 *      onApplicationEvent对消息进行接受处理
 * @author lyq
 */
@Component
public class DemoEventListener implements ApplicationListener<DemoEvent> {

    @Override
    public void onApplicationEvent(DemoEvent demoEvent) {
        String msg = demoEvent.getMsg();
        System.out.println("事件监听器监听到事件消息DemoEvent,消息内容为: "+msg);
    }
}

 

三、使用ApplicationContext来发布事件

      代码如下:

/**
 * 事件发布类
 *      使用ApplicationContext来发布事件
 * @author lyq
 */
@Component
public class DemoPublisher {

    @Autowired
    ApplicationContext applicationContext;

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

 

四、在main方法中new DemoPublisher进行发布(其中定义了EventConfig配置文件,用来自动扫描对应的包注入成bean对象)

      main方法类代码如下:

/**
 * @author lyq
 */
public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(EventConfig.class);
        DemoPublisher demoPublisher = applicationContext.getBean(DemoPublisher.class);
        demoPublisher.publish("我来发布消息");
        applicationContext.close();
    }
}

    配置类代码如下:

/**
 * @author lyq
 */
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch2.event")
public class EventConfig {

}

 

转载于:https://my.oschina.net/luoyaqi/blog/1630515

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过自定义事件来实现在特定情况下触发一些自定义的逻辑或功能。下面是一个演示如何调用自定义事件的例子: 首先,我们需要定义一个自定义事件类,例如`CustomEvent`,该类需要继承`ApplicationEvent`: ```java public class CustomEvent extends ApplicationEvent { private String message; public CustomEvent(Object source, String message) { super(source); this.message = message; } public String getMessage() { return message; } } ``` 然后,我们需要定义一个事件发布者类,例如`CustomEventPublisher`,该类负责发布自定义事件: ```java @Component public class CustomEventPublisher { @Autowired private ApplicationEventPublisher applicationEventPublisher; public void publishCustomEvent(String message) { CustomEvent customEvent = new CustomEvent(this, message); applicationEventPublisher.publishEvent(customEvent); } } ``` 接下来,我们可以在需要的地方注入`CustomEventPublisher`,并调用`publishCustomEvent`方法来发布自定义事件: ```java @Service public class MyService { @Autowired private CustomEventPublisher customEventPublisher; public void doSomething() { // 执行一些业务逻辑 // ... // 发布自定义事件 customEventPublisher.publishCustomEvent("Custom event message"); } } ``` 最后,我们需要定义一个事件监听类,例如`CustomEventListener`,该类负责监听并处理自定义事件: ```java @Component public class CustomEventListener implements ApplicationListener<CustomEvent> { @Override public void onApplicationEvent(CustomEvent customEvent) { // 处理自定义事件 String message = customEvent.getMessage(); System.out.println("Received custom event with message: " + message); } } ``` 通过以上步骤,我们就可以在Spring Boot中调用自定义事件了。当`MyService`中的`doSomething`方法被调用时,会触发自定义事件发布,然后`CustomEventListener`会监听到该事件并进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值