这个是spring基于Application的监听器,继承ApplicationEvent类就可以创建一个监听器监听的对象。具体代码如下:
这个是spring基于Application的监听器,继承ApplicationEvent类就可以创建一个监听器监听的对象。具体代码如下:
- 创建监听器监听的类
package com.example.demo.event;
import lombok.Data;
import org.springframework.context.ApplicationEvent;
/**
* 监听器监听的对象
*/
@Data
public class DemoEvent extends ApplicationEvent {
private String msg;
public DemoEvent(Object source,String msg) {
super(source);
this.msg = msg;
}
}
- 创建监听器监听 DemoEvent 事件
package com.example.demo.event;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
/**
* 监听DemoEvent事件
* @author javacfox
*/
@Configuration
public class DemoEventListener implements ApplicationListener<DemoEvent> {
@Override
public void onApplicationEvent(DemoEvent demoEvent) {
System.out.println("DemoEventListener接受到了demoPublisher发布的消息:"+demoEvent.getMsg());
}
}
- 事件发布
package com.example.demo.event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DemoEventPublisher {
@Autowired
private ApplicationContext applicationContext;
public void publish(){
DemoEvent event = new DemoEvent(this, "发布成功!");
System.out.println("发部event:"+event);
applicationContext.publishEvent(event);
}
}
- 测试
package com.test.demo.event;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan({"com.test.demo.event"})
public class EventTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventTest.class);
context.start();
String targetObj = "i am the target object";
DemoEvent demoEvent = new DemoEvent(targetObj,"目标发布成功!");
// 发布事件
context.publishEvent(demoEvent);
context.close();
}
}
- 测试结果
21:55:21.065 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@16f65612
21:55:21.079 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
21:55:21.129 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [F:\spring-boot-2.0.1.RELEASE\spring-boot-project\DemoTest\target\classes\com\test\demo\event\DemoEventListener.class]
21:55:21.130 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [F:\spring-boot-2.0.1.RELEASE\spring-boot-project\DemoTest\target\classes\com\test\demo\event\DemoEventPublisher.class]
21:55:21.207 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
21:55:21.209 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
21:55:21.210 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
21:55:21.211 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
21:55:21.217 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'eventTest'
21:55:21.221 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'demoEventListener'
21:55:21.222 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'demoEventPublisher'
DemoEventListener接受到了demoPublisher发布的消息:目标发布成功!
21:55:21.248 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@16f65612, started on Tue Jan 14 21:55:21 CST 2020
Process finished with exit code 0